All you need to know about Pivot Tables

Guide-to-laravel-Pivot-TAble

In this post, I am going to talk about many-to-many relationships and Pivot Tables. You heard about it, perhaps used it but you still don’t fully understand the concept, then keep on ready this post.  Pivot tables are very helpful when designing  database structure but can be difficult to understand sometimes. Pivot tables are intermediate tables that facilitates relationships between two main tables and even within one table.

When exactly you need Pivot Table

First of all, let’s clear our understanding of scenarios where Pivot Table can help us.  As I said  it provides many-to-many relationship between two tables, let’s look at some examples when exactly one need many-to-many-relationship and Pivot Table.

Categories-Posts Example

If you have used WordPress, you know that you can assign multiple categories to a post. So here, a post can reside in multiple categories and a category can have multiple posts in it. Technically

Post -> Belongs To Many -> Categories

Categories -> Belongs To Many -> Posts

that’s a very basic scenario where you will need pivot table. Here’s what the table structure will look like. Continue reading