Laravel 8 Many To Many Relationship Example

Websolutionstuff | Nov-15-2021 | Categories : Laravel PHP MySQL

In this example we will see laravel 8 many to many relationship example. Use many to many relationship in laravel 6 and laravel 7 and laravel 8. Many to many relationship is complicated than one to one and one to many relationships. example of like a relationship is a user with have multiple roles, where the role are also connected with multiple users.

Also we will create migration with foreign key, sync with a pivot table, retrieve records using model, insert new records, update records etc. We will create User and Roles and Role_users table.all tables are connected with each other table. Now we will create many to many relationships with each other by using the laravel Eloquent Model.

laravel_many_to_many_relationship

 

 

Create Migration

Now we have to create migration for users and roles and role_users table. we will also add foreign key with users and roles table.

Create Migration of Users Table

Schema::create('users', function (Blueprint $table) {

    $table->increments('id');

    $table->string('name');

    $table->string('email')->unique();

    $table->timestamps();

});

Create Migration of Roles Table

Schema::create('roles', function (Blueprint $table) {

    $table->increments('id');

    $table->string('name');

    $table->timestamps();

});

Create Migration of Role Users Table

Schema::create('role_users', 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');

});

 

 

Create Model and Add Relationship on Both Model

Now, we will create User, Role model. we will use belongsToMany() relationship of both model.

User Model :

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * Get the phone associated with the user.
     */
    public function roles()
    {
        return $this->belongsToMany(Role::class, 'role_users');
    }
}

 

Role Model :

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

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

 

Retrieve Records using Model :

Once the relationship is defined, we may retrieve the related record using Eloquent's dynamic properties.

$role = User::find(1)->roles;
$user = Role::find(1)->users;

 

Create Records using Model 
$user = User::find(1);	
 
$role_ids = [1, 2];
$user->roles()->attach($role_ids);
$user = User::find(3);	
 
$role_ids = [1, 2];
$user->roles()->sync($role_ids);

 


You might also like :

Recommended Post
Featured Post
How To Create Custom Login Page In Django
How To Create Custom Login Pag...

In this article, we will see how to create a custom login page in django. how to allow user registration, login, an...

Read More

May-14-2022

Datatable Custom Export Button Example
Datatable Custom Export Button...

Hello Guys, In this tutorial we will see how to add datatable custom export button example. datatable provide inbuilt...

Read More

Apr-26-2021

How to Integrate Razorpay Payment Gateway in Laravel
How to Integrate Razorpay Paym...

In this article, we will see the most important and exciting toping about how to integrate razorpay payment gateway in l...

Read More

Jan-06-2021

Laravel 8 Many To Many Polymorphic Relationship
Laravel 8 Many To Many Polymor...

In this tutorial we will see laravel 8 many to many polymorphic relationship. many to many polymorphic relationship more...

Read More

Nov-22-2021