Skip to content

Laravel Relationship

An Eloquent relationship is a very important feature in Laravel that allows you to relate the tables in a very easy format.

One to one relationship

One to one relationship provides the one-to-one relationship between the columns of different tables. For example, every user is associated with a single post or maybe multiple posts, but in this relationship, we will retrieve the single post of a user. To define a relationship, we need first to define the post() method in User model. In the post() method, we need to implement the hasOne() method that returns the result.

Let's understand the one to one relationship through an example.

First, we add the new column (user_id) in an existing table named as posts. Here, user_id is the foreign key.

create_A_record

1.Migrate the above changes in a database by using the command given below: php artisan migrate.

2.After the migration, the structure of the posts table is shown in the below screenshot:

create_A_record

The above screenshot shows that the user_id column is added successfully. Open the User.php file and add the following code in the User.php file.

public function post()  
{  
  return $this->hasOne('App\Post');  
}

n the above code, we have implemented the hasOne() method that contains the single argument, i.e., name of the related model. By default, the Post considers the user_id as a foreign key. The post() method searches the posts table as we have mentioned the namespace 'App/Post' and look for the column user_id. We can override this convention by providing a foreign key as a second argument. It can be re-written as:

return $this->hasOne('App\Post',foreign_key)

Now, we will add the route in the web.php file. <?php
use App\User;
Route::get('/user',function()
{
return User::find(1)->post;
}
);
The above code is finding the user with an id number 1 and then implementing the post to find the post of the user having user_id equal to 1.

create_A_record

Open the Post.php file(model) that we created earlier.

create_A_record

One-to-Many relationship

Laravel also provides a one-to-many relationship. A one-to-many relationship is used to define relationships where a single model is the parent to one or more child models.

First, we define the route that finds out all the posts of a single user.

  Route::get('/posts',function(){  
$user=User::find(1);  
foreach($user->posts as $post){  
echo $post->title."<br>";  
}  
});  
Add the following code in the User.php(model) file.

    public function posts()  
{  
    return $this->hasMany('App\Post','user_id');  
}

create_A_record

Many-to-Many relationship

A many-to-many relationship is more complicated than a one-to-one relationship and a one-to-many relationship. To define the many-to-many relationship, we need to create a pivot table. The pivot table is basically a look up table that relates the two tables. For example, a user may have different roles where the roles can be shared by other users, like many users can have the role of 'Admin'. To define the relationship between the users and roles, we need to create the three tables, user, roles, and role_user. In our database, the user table is already created; we need to create two tables, i.e., roles table and pivot table (roles_user).

First, we create the Role model. We use the following command to create the model: php artisan make:model Role -m

create_A_record

The above screen shows that the roles table has been created. The create_roles_table.php has been created in the database/migration directory. The structure of this file is given below:

create_A_record

In the above code, we have added the new column named as 'name'. The 'name' column defines the name of the role of a user.

Now, we have two tables, the roles table, and the users table. To relate these two tables, we need to create the pivot table, roles_user table.

create_A_record

The above screen shows that the roles_user table has been created. The structure of the create_roles_user_table is given below:

create_A_record

In the above code, we have added two new columns, user_id, and role_id.

Migrate all the above changes by using the command given below: php artisan migrate

create_A_record

Data available in the roles table:

create_A_record

Data available in the users table:

create_A_record

Data available in the roles_user table:

create_A_record

Now, we define the route.

Route::get('/roles/{id}',function($id){  
$user=User::find($id);  
foreach($user->role as $role)  
{  
   return $role->name;  
}  
});

We add the following code in User.php file which relates both the tables.

public function role()  
{  
   return $this->belongsToMany('App\Role','roles_user');  
}

In the above code, belongsToMany() method contains two parameters, 'App\Role', which is the namespace to use the Role model, and 'roles_user' is the name of the pivot table that relates two tables. The belongsToMany() method can also be written as

belongsToMany('App\Role','roles_user','user_id','role_id');

The above line contains two more parameters, user_id, and role_id. The user_id is the foreign key for the users table, and role_id is the foreign key for the roles table.

Output

create_A_record

create_A_record