Laravel Many to Many Eloquent Relationship Example

Today, We want to share with you Laravel Many to Many Eloquent Relationship Example.
In this post we will show you many to many relationship laravel 5.6, hear for laravel 5 save many to many relationship we will give you demo and example for implement.
In this post, we will learn about laravel 5 many to many sync with an example.

Laravel Many to Many Eloquent Relationship Demo

let’s start, I have to make database simple migration of main “employees”, “positions” and “position_employee” simple created table. I shall also put the main Laravel foreign key with employees and positions table. so let’s demo for Laravel Many to Many Eloquent Relationship make like as below:

READ :  Vue Drag and Drop Sortable Example - Vuejs Draggable

Create Migrations:

employees table migration:

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

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

position_employee table migration:
Schema::create('position_employee', function (Blueprint $table) {
    $table->integer('employee_id')->unsigned();
    $table->integer('position_id')->unsigned();
    $table->foreign('employee_id')->references('id')->on('employees')
        ->onDelete('cascade');
    $table->foreign('position_id')->references('id')->on('positions')
        ->onDelete('cascade');
});

Create Models:

User Model:

belongsToMany(Role::class, 'position_employee');
    }
}

Role Model:
belongsToMany(User::class, 'position_employee');
    }
}

UserRole Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserRole extends Model
{
     
}

Retrieve Records:
$employee = User::find(1);	
 
dd($employee->positions);

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

Make Records:
$employee = User::find(2);	
 
$roleIds = [1, 2];
$employee->positions()->attach($roleIds);

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

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

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

jQuery 15 Powerful Tips and Tricks for Developers and Web Designer

Read :

READ :  Vuejs Upload using http post and FormData - vue file upload component

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel 5 many to many attach.
I would like to have feedback on my Infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Add a Comment

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