Laravel (10) Pivots: The Secret Sauce for Powerful Relationships You Won't Believe!

Ashutosh Kukreti

In Laravel, pivot tables serve as intermediary database tables that facilitate many-to-many relationships between two other tables. Acting as connectors, they store additional information about the relationship itself. Pivot tables contain foreign keys referencing the primary keys of the related tables, along with any extra columns necessary for relationship-specific data. By utilizing pivot tables, developers can seamlessly manage and query many-to-many relationships in Laravel, leveraging the powerful features provided by Laravel's Eloquent ORM. This simplifies the establishment and handling of complex relationships between models, enhancing the efficiency and flexibility of the application.

To illustrate this, imagine we have two models: User and Role. A user can have multiple roles, while a role can be assigned to multiple users. To represent this relationship, we create a pivot table named role_user.

Set up the User model:

class User extends Model
{
    public function roles()
    {
        return $this->belongsToMany(Role::class)->withTimestamps();
    }
}

Set up the Role model:

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

Create the migration for the role_user pivot table:

php artisan make:migration create_role_user_table --create=role_user

By defining the relationships in the respective model classes, we can effortlessly interact with pivot tables.

For instance, the User model would have a roles() method defined using the belongsToMany() function, specifying the Role model. Similarly, the Role model would have a users() method defined in a similar manner.

Define the migration for the role_user table

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateRoleUserTable extends Migration
{
    public function up()
    {
        Schema::create('role_user', function (Blueprint $table) {
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('role_id');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
        });
    }

    public function down()
    {
        Schema::dropIfExists('role_user');
    }
}

To represent the relationship between users and roles, we create a pivot table named role_user with columns user_id and role_id. These columns establish the connections between users and roles.

Now, let's explore how we can interact with pivot tables. To retrieve the roles associated with a user, we can simply use the roles() method:

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

Additionally, we can access the timestamps stored in the pivot table using the pivot property:

foreach ($roles as $role) {
    echo $role->name;
    echo $role->pivot->created_at;
}

To add or remove records in the pivot table, we can use the attach() and detach() methods:

$user->roles()->attach($roleId);    // Add a role
$user->roles()->detach($roleId);    // Remove a role

Laravel's pivot tables provide an elegant solution for managing many-to-many relationships by storing and retrieving associated data efficiently.

Lets take another example

Consider a scenario where we have two models: Product and Category. A product can belong to multiple categories, and a category can have multiple products. To represent this many-to-many relationship, we need a pivot table called category_product to establish the connections.

In the Product model, define the relationship with the Category model:

class Product extends Model
{
    public function categories()
    {
        return $this->belongsToMany(Category::class)->withTimestamps();
    }
}

In the Category model, define the relationship with the Product model.

class Category extends Model
{
    public function products()
    {
        return $this->belongsToMany(Product::class)->withTimestamps();
    }
}


Next, create the migration for the category_product pivot table:

php artisan make:migration create_category_product_table --create=category_product

Define the migration for the category_product table:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCategoryProductTable extends Migration
{
    public function up()
    {
        Schema::create('category_product', function (Blueprint $table) {
            $table->unsignedBigInteger('category_id');
            $table->unsignedBigInteger('product_id');
            $table->timestamps();

            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
        });
    }

    public function down()
    {
        Schema::dropIfExists('category_product');
    }
}

To access the related data, you can use the pivot table methods:

$product = Product::find(1);
$categories = $product->categories;

foreach ($categories as $category) {
    echo $category->name;
    echo $category->pivot->created_at;
}

You can also add or remove records in the pivot table:

$product->categories()->attach($categoryId);    // Add a category
$product->categories()->detach($categoryId);    // Remove a category

By utilising pivot tables, Laravel allows you to manage many-to-many relationships seamlessly, retrieve related data, and perform association and disassociation operations effortlessly.

See in the next blog

LaravelBeginners