laravel relationships many to many to many code example
Example 1: many to many relationship laravel
use App\Models\User;
$user = User::find(1);
$user->roles()->attach($roleId);
Example 2: laravel many to many relation update
$user->roles()->sync([1, 2, 3]);