In this tutorial, we will learn how to update the pivot table records in Laravel. While working with Many to Many relationships in Laravel we need to introduce an intermediate table which is called the
Pivot table in Laravel terms.
pivot attribute we can easily interact with this intermediate table on the models or in controllers.
Example Of Pivot Table In Laravel
Let’s say in our project, we need to define the roles of every user. So users can have many roles and inverse roles can have many users. Thus, it will be a Many To Many relationships.
To accomplish this we need to create 3 tables
roles, and intermediate table
role_user table will have the
role_id column which connects both users and roles table and this intermediate table called the pivot table.
Here are the table structures:
users id - integer name - string roles id - integer name - string role_user user_id - integer role_id - integer
We have the following data in our database
id name email -- ------- ------- 1 John Doe 1 firstname.lastname@example.org 2 John Doe 2 email@example.com 3 John Doe 3 firstname.lastname@example.org 4 John Doe 4 email@example.com
id name created_at updated_at -- ---------- ------------------- ------------------ 1 Admin 2021-05-27 13:00:32 2021-05-27 13:00:32 2 Subscriber 2021-05-27 13:00:32 2021-05-27 13:00:32 3 Editor 2021-05-27 13:00:32 2021-05-27 13:00:32 4 Viewer 2021-05-27 13:00:32 2021-05-27 13:00:32
id user_id role_id -- ------- ------- 1 1 1 2 2 2 3 3 2 4 4 3
Update Pivot Table In Laravel
We have 3 tables as mentioned above but we have only two models
Role.php but what about 3rd one? We don’t have that right? But the twist here, we can interact with the third
role_user table using the following ways:
01 Using attach() Method
As per our database entry, For a user with
ID 2 has a
Subscriber role, we want to assign an additional role
Editor to that user. Let’s do that.
The attach() method will simply insert new records into the intermediate(pivot) table.
<?php use App\Models\User; $user = User::find(2); $roleID = 3; // Editor role ID $user->roles()->attach($roleID);
We can also pass the array of IDs to attach multiple roles to the user.
02 Using detach() Method
The detach() method is inverse of attach() method, it will simply remove the records from the intermediate(pivot) table.
e.g. We recently attached a new role
Editor to the user with
ID 2. Let’s remove that role from that user.
<?php use App\Models\User; $user = User::find(2); $roleID = 3; // Editor role ID $user->roles()->detach($roleID);
We can also pass the array of IDs to remove multiple roles from the user.
Notes: if you don’t pass anything in
detach() method then it will remove all the entries e.g.
03 Using sync() Method
The sync() method accepts an array as an argument. As the name suggests, this method synchronizes the database entries that means whatever you pass in this method, those records will be kept into the database and the rest will be removed from the intermediate(pivot) table.
e.g. If a user with
ID 2 has 3 roles,
Viewer. Now, we want to remove the
Viewer role for that user but we want to retain the
role_user table before
id user_id role_id -- ------- ------- 1 1 1 2 2 2 3 2 3 4 2 4 5 3 2 6 4 3
<?php use App\Models\User; $user = User::find(2); // Want to keep only Editor (Id 2) role $user->roles()->sync();
After performing the above operation, our
role_user table will look like below:
id user_id role_id -- ------- ------- 1 1 1 2 2 2 5 3 2 6 4 3
04 Using toggle() Method
As the name suggests, the toggle() method will “toggles” the IDs that means if you provide an array of IDs then if any of the IDs exist, it will remove those IDs from the database and If some of the provided IDs do not exist then it will insert the IDs.
e.g. If a user with
ID 2 has 2 roles,
Subscriber. Now, we want to remove both roles and want to add
Viewer role for that user.
role_user table before
id user_id role_id -- ------- ------- 1 1 1 2 2 2 3 2 3 5 3 2 6 4 3
<?php use App\Models\User; $user = User::find(2); $user->roles()->toggle([2,3,4]);
role_user table after
id user_id role_id -- ------- ------- 1 1 1 5 3 2 6 4 3 7 2 4
The above example has removed the entries with ID 2 & 3 and inserted new records with ID 7.
Additionally, read our guide:
- Base Table Or View Already Exists In Laravel Migration
- Add Column After A Column In Laravel Migration
- Laravel: Change Column Type In Migration
- Laravel: Change Column Name In Migration
- How To Use Where Date Between In Laravel
- How To Add Laravel Next Prev Pagination
- Laravel Remove Column From Table In Migration
- Laravel: Get Month Name From Date
- Laravel: Increase Quantity If Product Already Exists In Cart
- How To Calculate Age From Birthdate
- How To Dynamic iFrame URL In Elementor
- How To Handle Failed Jobs In Laravel
- How To Remove WooCommerce Data After Uninstall
- How To Get Latest Records In Laravel
- How To Break Nested Loops In PHP Or Laravel
- How To Pass Laravel URL Parameter
- Laravel Run Specific Migration
- How To Fix Elementor Icons Not Showing
That’s it from our end. We hope this article helped you to update pivot table in Laravel.
Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!