update many to many relationship laravel 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]);
Example 3: laravel many to many update all pivot
$user->customviews()
->newPivotStatement()
->where('user_id', '=', $user->id)
->update(array('default' => 0));
Example 4: laravel many to many relationship
class User extends Model
{
public function roles()
{
return $this->belongsToMany(Role::class,'role_user');
}
}
class Role extends Model
{
public function users()
{
return $this->belongsToMany(User::class);
}
}