Complete the code to define a many-to-many relationship in a Laravel model.
public function roles() {
return $this->[1](Role::class);
}In Laravel, many-to-many relationships are defined using the belongsToMany method.
Complete the code to attach a role to a user using the many-to-many relationship.
$user->roles()->[1]($roleId);The attach method adds a record to the pivot table linking the two models.
Fix the error in the code to retrieve all roles of a user.
$roles = $user->[1];The relationship method is named roles, so accessing it as a property returns the collection of roles.
Fill both blanks to define a many-to-many relationship with a custom pivot table and foreign keys.
public function permissions() {
return $this->[1](Permission::class, '[2]', 'user_id', 'permission_id');
}Use belongsToMany to define the relationship and specify the pivot table name permission_user.
Fill all three blanks to sync roles for a user, replacing existing links.
$user->[1]()->[2]([[3]]);
Use the roles relationship, call sync to replace existing roles, and pass an array of role IDs.