In this tutorial you will learn about the Laravel Eloquent Relationships Example and its application with practical example.
In this Laravel Eloquent Relationships example tutorial, I’ll show you how to create and use eloquent relationships in laravel database model. In this article we will learn to implement and use different types of eloquent model relation available in laravel. We will also learn to perform create, insert and to delete operations in different type of relationships.
Type of Laravel Eloquent Relationships
In laravel, there are following types of eloquent relationships available:
- Laravel eloquent one to one example
- Laravel eloquent one to many example
- Laravel Eloquent Many to Many Relationship Example
- Laravel Eloquent HasMany Through Relationship Example
Laravel One to One Relationship Example
The one to one relationship is a very common relationship in laravel model. In this example we will learn to implement and use one to one relationship in laravel. We will also show you how to insert, retrieve, update, and delete data with the eloquent model from the database table in laravel.
Laravel Eloquent One to One Relationship Example
Let suppose we have two tables posts and contents in our example tutorial. In laravel we can create both tables using migration as following:
Post table migration:
1 2 3 4 5 6 7 8 9 10 11 |
Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->text('short_desc'); $table->timestamps(); }); |
Contents table migration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Schema::create('contents', function (Blueprint $table) { $table->increments('id'); $table->integer('post_id')->unsigned(); $table->text('description'); $table->timestamps(); $table->foreign('post_id')->references('id')->on('posts') ->onDelete('cascade'); }); |
In this example, the Post model is closely associated with a Content model in one to one relationship. In one to one relationship we will create a post_content() method within the Post model and there is hasOne() method to relate it with Content 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 Post extends Model { public function post_content() { return $this->hasOne('App\Content'); } } |
The Eloquent model adds a foreign key based on the model name and should have a matching id value in it. In this relationship the post_id is the foreign_key for Content model,
Inverse of One to One Relationship Example
For now we can have the access to the content from the post model. Now we create an inverse relationship in the content model. For this we will use the belongsTo method for getting the post data from the content 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 Content extends Model { public function post() { return $this->belongsTo('App\Post'); } } |
We can now access the post content using the relation method as following:
1 |
$content = Post::find(10)->post_content; |
Laravel One to Many Relationship Example
In this step by step tutorial I’ll guide you through to create many to many relationship and learn how to use this relationship. In this example we will use one to many relationship, and perform crud (create, read, update, delete) operation with the eloquent model from the database table in laravel.
Laravel Eloquent One to Many Relationship Example
Let suppose we have tables name posts and authors. Using laravel migration, you can create both tables with the following fields:
Author table migration:
1 2 3 4 5 6 7 8 9 |
Schema::create('authors', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->timestamps(); }); |
Post table migration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->text('description'); $table->integer('author_id')->unsigned(); $table->timestamps(); $table->foreign('author_id')->references('id')->on('authors') ->onDelete('cascade'); }); |
Define One To Many Relationship
Now we will define One To Many Relationship for the model we have created. In One to many relationships means a relationship where one single model can owns the number of the other model. As per the example model we created, a single author can have written many post articles. Let’s take an example of one to many relationships.
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'); } } |
Now, we can access the collection of post articles by using the post() method as.
1 2 3 4 5 |
$posts = App\Author::find(10)->post()->get(); foreach ($posts as $post) { //do something } |
Defining Inverse One To Many Relationship in Laravel
As of now we can access all the post articles of an author, now suppose if we want to allow a post model to access its Author model or access author details by using the post model. The inverse relationship of both One to One and One to Many works the same way. The inverse of the one to many relationship can be defined using the belongsTo method. Thus we can define in example mode 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 in one to many relationship as following:
1 |
$post->author->name; |
Retrieving Data In One to many relationship
In one to many relationship you can retrieve data from model as following:
You can get all posts of a particular author:
1 2 3 4 5 |
$posts = Author::find(10)->post()->get(); foreach ($posts as $post) { //do something } |
Similarly, you can retrieve the inverse related model.
1 2 3 4 5 |
$posts = Post::find(10)->author()->get(); foreach ($posts as $post) { //do something } |
Laravel Many to Many Relationship Example
In laravel using belongsToMany() method, you can define many to many relationship in laravel eloquent models. Then you can insert, update and delete data from table using many to many relationship in laravel.
Laravel Eloquent Many to Many Relationship Example
In this example we will demonstrate the implementation of Many to Many Relationship using two tables posts and tags. In this relationship each of the post can have many tags and each of the tag can be associated with many posts.
Now, lets understand how to define many to many relationships, 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 the post model as follow:
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
In Laravel, the inverse relationship of a many to many relationships can be defined using belongsToMany method on the reverse model.
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'); } } |
Now you can access the post in the tag model as follow:
1 2 3 4 5 |
$tag = App\Tag::find(8); foreach ($tag->posts as $post) { //do something } |
Laravel Eloquent HasMany Through Relationship Example
In this Laravel Has Many Through Eloquent Relationship tutorial we will implement an example for the Laravel Has Many Through Eloquent Relationship. We will generate migration files of “users”, “posts” and “countries” table and then add a foreign key with users and posts table as following:
In users migration file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->integer('country_id')->unsigned(); $table->rememberToken(); $table->timestamps(); $table->foreign('country_id')->references('id')->on('countries') ->onDelete('cascade'); }); |
In posts migration file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->string("name"); $table->integer('user_id')->unsigned(); $table->timestamps(); $table->foreign('user_id')->references('id')->on('users') ->onDelete('cascade'); }); |
In countries migration file:
1 2 3 4 5 6 7 8 9 |
Schema::create('countries', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->timestamps(); }); |
In laravel, the “has-many-through” relationship provides a shortcut for accessing distant relations via an intermediate model relation. For example, a Country
model might have many Post
models through an intermediate User
model. In this example, we can easily get all blog posts of a given country. I’ll show you how to define HasMany Through 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'); } } |
To fetch data using this relationship as follow:
1 2 3 |
$country = Country::find(1); dd($country->posts); |