Suppose users can belong to many teams and teams can contain many users.
users
teams
team_user
User model:
public function teams()
{
return $this->belongsToMany(Team::class);
}
Team model:
public function users()
{
return $this->belongsToMany(User::class);
}
Usage:
$user->teams;
Attach:
$user->teams()->attach($teamId);
Detach:
$user->teams()->detach($teamId);
Sync:
$user->teams()->sync([1, 2, 3]);
Scenario
This is common for:
Users ↔ Roles
Students ↔ Courses
Products ↔ Categories
Posts ↔ Tags
0 comments:
Post a Comment
Thanks