Laravel – How to Setup Many to Many Eloquent Relationships?

In this tutorial, we will explore how to create and manage many-to-many relationships in Laravel using Eloquent ORM. We will cover creating migrations with foreign keys, using the sync method with a pivot table, creating, retrieving, updating, and deleting records, and applying conditions to our queries.   Understanding Many-to-Many Relationships Many-to-many relationships are slightly more…

By.

min read

In this tutorial, we will explore how to create and manage many-to-many relationships in Laravel using Eloquent ORM. We will cover creating migrations with foreign keys, using the sync method with a pivot table, creating, retrieving, updating, and deleting records, and applying conditions to our queries.

 

Understanding Many-to-Many Relationships

Many-to-many relationships are slightly more complex than one-to-one and one-to-many relationships. In a many-to-many relationship, multiple records from one table can be associated with multiple records from another table.

 

Example Scenario

A common example of a many-to-many relationship is users with multiple roles, where each role can also be associated with multiple users. This scenario is often seen in applications where users can have different permissions or capabilities based on their assigned roles.

 

Database Structure

In this example, we will create three tables: users, roles, and role_user. Each table will be interconnected, and we will establish many-to-many relationships between the tables using Laravel’s Eloquent Model.

 

Tables

  1. users
  2. roles
  3. role_user (pivot table)

 

Many-to-Many Relationship Implementation

To implement a many-to-many relationship, we will use the belongsToMany() method in our Eloquent models.

 

Creating Migrations

We will begin by creating migrations for the users, roles, and role_user tables. We will also define foreign key relationships between the users and roles tables.

Users Table Migration

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('email')->unique();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
});

Roles Table Migration

Schema::create('roles', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->timestamps();
});

Role_User Table Migration

Schema::create('role_user', function (Blueprint $table) {
    $table->integer('user_id')->unsigned();
    $table->integer('role_id')->unsigned();
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
});

 

Creating Models

Next, we will create Eloquent models for the User, Role, and UserRole tables. We will use the belongsToMany() method to define the relationship between the User and Role models.

User Model

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    public function roles()
    {
        return $this->belongsToMany(Role::class, 'role_user');
    }
}

Role Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
    public function users()
    {
        return $this->belongsToMany(User::class, 'role_user');
    }
}

UserRole Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UserRole extends Model
{
}

Retrieving Records

To retrieve the roles associated with a specific user, and vice versa, you can use the following code snippets:

Retrieving Roles for a User

$role = Role::find(1);
dd($role->users);

Creating Records

To create records and associate them with the relevant roles or users, you can use the attach() and sync() methods:

Attaching Roles to a User

$user = User::find(2);
$roleIds = [1, 2];
$user->roles()->attach($roleIds);

Syncing Roles for a User

$user = User::find(3);
$roleIds = [1, 2];
$user->roles()->sync($roleIds);

Attaching Users to a Role

$role = Role::find(1);
$userIds = [10, 11];
$role->users()->attach($userIds);

Syncing Users for a Role

$role = Role::find(2);
$userIds = [10, 11];
$role->users()->sync($userIds);

 

Frequently Asked Questions

 

What is the purpose of the pivot table in a many-to-many relationship?

The pivot table is used to establish connections between the two main tables in a many-to-many relationship. It stores the unique identifiers of the related records in both tables.

 

Can I use the attach() and sync() methods interchangeably?

While both methods can be used to associate records, attach() simply adds new relationships without affecting existing ones, whereas sync() updates the relationships by adding new ones and removing any that are not specified in the provided array.

 

What is the belongsToMany() method used for?

The belongsToMany() method is used in Eloquent models to define a many-to-many relationship between two tables.

 

Do I need to create a separate model for the pivot table?

While it’s not always necessary, creating a separate model for the pivot table can be beneficial if you need to define additional relationships or add custom methods related to the pivot table.

 

Can I add additional fields to the pivot table?

Yes, you can add additional fields to the pivot table to store extra information about the relationship. To access these fields, you can use the withPivot() method in your Eloquent models when defining the relationship.

 

Conclusion

We hope this tutorial has provided a clear understanding of how to create and manage many-to-many relationships in Laravel using Eloquent ORM. By following these steps, you can efficiently handle complex relationships in your application and improve overall code quality.

Leave a Reply

Your email address will not be published. Required fields are marked *