Databases Archives -

What’s a pivot table?

A pivot table is a database table that only exists to serve a many-to-many relationship. Say you have a table “customer” and a table “drinks”. If you want to know which customer ordered which drink you have to create a pivot table customer_drinks(customer_id, drink_id).

Laravel can handle these tables (semi)automatically.

Define the pivot table in Laravel

If you want to define the pivot table in Laravel, you have to create a belongsToMany relationship. Example:

Continue reading “Laravel 4: pivot table example (attach and detach)”…