1$user = User::find(2);
2$roleIds = [1, 2];
3$user->roles()->attach($roleIds);
4
5$user = User::find(3);
6$roleIds = [1, 2];
7$user->roles()->sync($roleIds);
1$employee = employee::find(1);
2
3$salary1 = new Salary;
4$salary->amount = '123456789';
5$salary->payment_date = '15/07/2020';
6
7$salary2 = new Salary;
8$salary->amount = '123456789';
9$salary->payment_date = '16/07/2020';
10
11$employee = $employee->salary()->saveMany([$salary1, $salary2]);
11. $user->roles()->attach($roleId);
2
32. you may also pass an array of additional data to be inserted
4$user->roles()->attach($roleId, ['expires' => $expires]);
5
63. // Detach a single role from the user...
7$user->roles()->detach($roleId);
8
94. // Detach all roles from the user...
10$user->roles()->detach();
11
125. Any IDs that are not in the given array will be removed from the intermediate
13 table
14$user->roles()->sync([1, 2, 3]);
15
166. If you need to update an existing row in your pivot table, you may use
17 updateExistingPivot method. This method accepts the pivot record foreign
18 key and an array of attributes to update:
19
20$user->roles()->updateExistingPivot($roleId, $attributes);