Many-to-Many means many records in one table can relate to many records in another table.

  • Ali has roles → Admin, Editor 
  • Sara has role → Editor 
  • The pivot table (role_user) connects users and roles.

User Model:

public function roles()
{
    return $this->belongsToMany(Role::class);
}

Role Model:

public function users()
{
    return $this->belongsToMany(User::class);
}