In this tutorial you will learn about the Laravel Relationships Cheat Sheet and its application with practical example.
In this Laravel Eloquent Relationships with example tutorial, I’ll show you how to create and use eloquent relationships in your laravel database model. In laravel , you can define Eloquent Relationships model classes. In eloquent model you can create custom relationship methods along with methods example hasOne(), hasMany(), belongsTo(), belongsToMany(), hasOneThrough() etc.
- Eloquent Relationships in Laravel
- Laravel One to One Relationship with Example
- Laravel inverse of One to One Relationship Example
- Laravel One to Many Relationship Example
- Inverse One to Many Relationship Example
- Laravel Many to Many Relationship Example
- The inverse of Many to Many Relationship Example
- Laravel HasMany Through Relationship
Eloquent Relationships in Laravel
In laravel eloquent model you are allowed to create following type for relationships.
Types of Eloquent Relationships in Laravel
- One To One
- One To Many
- Many To Many
- HasMany Through
- Many To Many Polymorphic Relation
Laravel One to One Relationship with Example
In Laravel, one to one relationship is one of basic relationship. Suppose, Post model would be associated with a content model using one to one relationship. This one to one relationship means every post can have one one content associated with it. To demonstrate this relationship, we will create a post_content() method within the Post model and call the hasOne() method to relate the Content model.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Post extends Model { public function post_content() { return $this->hasOne('App\Content'); } } |
It is important to note that Eloquent establishes a foreign key based on the model name and should have a matching value id. Here post_id is the foreign_key of Content.
Laravel inverse of One to One Relationship Example
For now you are allowed to access the content from the post model. Now create an inverse relationship on the content model so that the post can accessed the content model . This can be achieved using laravel belongsTo method to get the post data from the content model.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Content extends Model { public function post() { return $this->belongsTo('App\Post'); } } |
you can access the post content using the relation method as following:
1 |
$content = Post::find(10)->post_content; |
Laravel One to Many Relationship Example
In One of many relationships one single model can associated with multiple number of the other model. For example, an author may have many post.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Author extends Model { ... public function post() { return $this->hasMany('App\Post'); } } |
you can now access the collection of post article by using the post() method as.Using this, we get all the posts of a single author. Here we need to get the author details of a single post,for each post there a single author, within the Post model includes belongsTo relation.
1 2 3 4 5 |
$posts = App\Author::find(10)->post()->get(); foreach ($posts as $post) { //do something } |
Inverse One to Many Relationship Example
The inverse of the one to many relationship is defined using laravel belongsTo method. Since we have access to post articles of an author, now we need to allow a post article to access its parent Author model or access author details by using the post model. This inverse relationship can be established using belongsTo method as following:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Post extends Model { public function author() { return $this->belongsTo('App\Author'); } } |
Now we can get the author details by using posts as following:
1 |
$post->author->author_name; |
Laravel Many to Many Relationship Example
Let suppose now each of the post have multiple tags and each tags can belongs to multiple posts then this type of relationship is defined as many to many relationship. In laravel many to many relationship can be defined using belongsToMany() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Post extends Model { ... public function tags() { return $this->belongsToMany('App\Tag'); } } |
Now you can access the tags in post model as following:
1 2 3 4 5 |
$post = App\Post::find(8); foreach ($post->tags as $tag) { //do something } |
The inverse of Many to Many Relationship Example
The inverse of a many to many relationships can also be defined using belongsToMany method on the associated model as following:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Tag extends Model { public function posts() { return $this->belongsToMany('App\Post'); } } |
you can now access the post in tag model as following:
1 2 3 4 5 |
$tag = App\Tag::find(8); foreach ($tag->posts as $post) { //do something } |
Laravel HasMany Through Relationship
The “has-many-through” relationship allows you accessing distant relations via an intermediate model. For example, a Country
model might have many Post
models through an intermediate User
model. In this example, you could easily gather all blog posts for a given country. Let’s look at the tables required to define this relationship:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Country extends Model { /** * Get all of the posts for the country. */ public function posts() { return $this->hasManyThrough('App\Post', 'App\User'); } } |