Courses

Structuring Databases in Laravel 11

Pivot Tables and Extra Field Operations

Summary of this lesson:
- Adding extra fields to pivot tables
- Managing pivot relationships
- Implementing timestamps in pivot tables
- Querying through pivot relationships

Let's discuss a feature of Laravel that is useful but potentially difficult to understand at first. A pivot table is an example of an intermediate table with relationships between two other "main" tables.


Real-life Example of Pivot Tables

Let's say a company has a dozen Shops all over the city/country and a variety of products, and they want to store information about which Products are sold in which Shops. It's a perfect example of a many-to-many relationship: one product can belong to several shops, and one shop can have multiple products.

So here's a potential database structure:

The product_shop table is called a "pivot" table.


Managing Many-to-Many Relationships: attach-detach-sync

How do we save the data with the help of our two Models instead of the third intermediate one? A couple of things here. For example, if we want to add another product to the current shop instance, we use relationship function and then method attach():

$shop = Shop::find($shop_id);
$shop->products()->attach($product_id);

A new row will be added to the product_shop table with $product_id and $shop_id values. Likewise, we can detach a relationship - let's say we want to remove a product from the shop:

$shop->products()->detach($product_id);

Or, more brutally, remove all products from a particular shop - then call the method without parameters:

$shop->products()->detach();

You can also attach and detach rows by passing an array of values as parameters:

$shop->products()->attach([123, 456, 789]);
$shop->products()->detach([321, 654, 987]);

Another beneficial function, in my experience, is updating the whole pivot table. For example, your admin area has checkboxes for shops for a particular product. During the Update operation, you must check all shops, delete those not in the new checkbox array, and then add/update the existing ones. Pain in the neck. Not anymore - there's a method called sync() which accepts new values as parameters array and then takes care of all that "dirty work" of syncing:

$product->shops()->sync([1, 2, 3]);

Result - no matter what values were previously in the product_shop table. After this call, there will be only three rows with shop_id equals 1, 2, or 3.


Additional Columns in Pivot Tables

As mentioned above, you would want more fields in that pivot table. In our example, saving the amount of products and price in that particular shop and timestamps makes sense. We can add the fields through migration files, as usual, but for proper usage in relationships, we have to make...

The full lesson is only for Premium Members.
Want to access all 18 lessons of this course? (81 min read)

You also get:

  • 73 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord