Backend Development

Demonstrating Laravel Many To Many Relationship

laravel many to many relationships

Laravel simplifies manipulating relationships in a very easy way using the eloquent ORM and in this article we will implement the many to many relationship using a simple example.

 

 

As we already know there are three main types of mysql relationships which are (One To OneOne To ManyMany To Many). For manipulating the first two relationships in laravel this is so simple as you have to add methods such as belongsTo() and hasMany() that describe the relations between the models for example if we imagine that we have a database that have two tables users and posts and each user can create many posts and the post belong only to single user so to describe this relation in laravel models:

app>User.php

<?php

namespace App;

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

class User extends Authenticatable
{
    use Notifiable;
    
    public function posts()
    {
        return $this->hasMany('App\Post');
    }
}

app>Post.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

In contrast for manipulating many to many relations typically we must have three tables [The first table – the second table – the pivot table (which is the table connecting the two tables by adding each primary key of the first and second tables)]. In laravel to describe such relationship we use method called belongsToMany().

Let’s take an example of an ecommerce website where we have categories and brands tables like the Clothes category may belong to multiple brands like Addidas, Dubles,etc. and in the other hand the Addidas brand may belong to other category such as Electronic devices category so the category has many brands and brands belongs to many categories so to implement such relationship let’s consider we have category and brand:

app>Category.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $table = "categories";

    public function brands()
    {
        return $this->belongsToMany('App\Brand');
    }
}

app>Brand.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Brand extends Model
{
    protected $table = "brands";

    public function categories()
    {
        return $this->belongsToMany('App\Category');
    }

}

As you see above i have added two methods on both models and each method should return belongsToMany() results of the other model so for categories we have method brands() that return belongsToMany(‘App\Brand’) and for brands we have method categories() that return belongsToMany(‘App\Category’), this tells laravel that this is a many to many relation between categories and brands.

Now to retrieve the brands for specific category we can do this:

$category = App\Category::find(1);

foreach ($category->brands as $brand) {
    //echo $brand->title;
}

To retrieve the categories for specific brand we can do this:

$brand = App\Brand::find(3);

foreach ($brand->categories as $category) {
    //echo $category->title;
}

You can chain query methods like this:

// get brands for category and order by title
$brands = App\Category::find(1)->brands()->orderBy('title')->get();

 

Customizing the pivot table name and foreign keys

The belongsToMany() method automatically determine the name of the joining table using the alphabetical order of the two related tables but if you want to override this behavior by passing a second argument to belongsToMany() with the table name like this:

return $this->belongsToMany('App\Brand', 'brand_category');

Also you can customize the foreign key names by passing two more arguments to belongsToMany(). The third argument is the foreign key name of the model on which you are defining the relationship, while the fourth argument is the foreign key name of the model that you are joining to like this:

app>Category.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    public function brands()
    {
        return $this->belongsToMany('App\Brand', 'brand_category','category_id', 'brand_id');
    }
}

app>Category.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Brand extends Model
{
    protected $table = "brands";

    public function categories()
    {
        return $this->belongsToMany('App\Category', 'brand_category', 'brand_id', 'category_id');
    }

}

 

0 0 vote
Article Rating

What's your reaction?

Excited
0
Happy
0
Not Sure
0
Confused
0

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments