For getting every new tutorial link Please join our telegram group

Part 6 laravel many to many relationship with pivot table example also video tutorial in hindi

This laravel eloquent relationship tutorial is very important here we will discuss many to many relationship by belongsToMany() method with hindi video.

Also ask laravel belongsToMany() relationship not working, example of many to many relationship, laravel eloquent belongsToMany relationship tutorial, laravel many to many relationship with pivot table and how to connect two tables by many to many relationship.

Laravel many to many relationship using belongsToMany() method with hindi video.

many to many relationship some complicated to other type of relationship. this relationship define when a table one row relate to many rows of second table and second table one row relate to many row of first table than we use a third pivot table to define many to many relationship.

Tables
tags => id, title
posts => id, title, content
post_tag => tag_id, post_id

In above example we define many to many relationship in tags and posts table one tag have many posts and one post include many tags, so we define a third table in alphabetical order for other two tables it is called pivot table. here our pivot table is post_tag and it take both tables primary key value.

Model structure of many to many relationship

In both models we define belongsToMany() relationship for getting each other related data see in below example.

Tag model
<?
namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Tag extends Model {
    
    public function posts() {
        return $this->belongsToMany(Post::class);
    }
}

Inverse of many to many relationship

Inverse of many to many relationhip same working this model also use belongsToMany() relationship for getting other table data.

Post model
<?
namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Post extends Model {
    
    public function tags() {
        return $this->belongsToMany(Tag::class);
    }
}

If our pivot table name not in alphabetical order and change other name than we pass table name in second argument

Post model
<?

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Post extends Model {
    
    public function tags() {
        return $this->belongsToMany(Tag::class, video_tag);
    }
}

In PostController or TagController we get tags and posts data. See below a PostController example where we get tags data of a post.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;

class PostController extends Controller{
    public function index(){
       $posts = Post::with('tags')->get();
        dd($posts);

        $post = Post::with('tages')->find(1);
        dd($post->tags);
    }
}

Create record in many to many relationship pivot table

We use two methods for creating record in third pivot table attach and sync method. Here attach method add new rows for ids that are pass in method not change in old record and sync add only rows that ids are given in method and other related records delete.

$post = Post::find(1);    
 
$tagIds = [1, 2];
$post->tags()->attach($roleIds);
//or sync method
$post->tags()->sync($roleIds);

If we want to add posts in tag

$tag = Tag::find(1);    
 
$postIds = [1, 2, 3];
$tag->posts()->attach($postIds);
//or sync method
$tag->posts()->sync($postIds);

In above example attach method add post id 1,2,3 on tag which id is 1 and not effect other data which are relate to this tag. whereas sync method add same ids but it delete all old data which are relate to this id 1 tag.

You read this laravel tutorial on advanced web tutorial. here we provide laravel beginners to advanced tutorial in hindi video.

php laravel developer anmol sharma

Anmol Sharma

I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.

Related tutorial links