1$post = App\Models\Post::find(1);
2
3$comment = $post->comments()->create([
4 'message' => 'A new comment.',
5]);
6
7--------------- OR ------------------
8
9$post->comments()->createMany([
10 [
11 'message' => 'A new comment.',
12 ],
13 [
14 'message' => 'Another new comment.',
15 ],
16]);
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);