Let me show the scenario here from the Laravel doc: relevant database tables are users, roles and role_users. Table names are self explanatory.
User model:
class User extends Model
{
/**
* The roles that belong to the user.
*/
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
The doc goes on to say :
As mentioned previously, to determine the table name of the relationship's joining table, Eloquent will join the two related model names in alphabetical order. However, you are free to override this convention. You may do so by passing a second argument to the belongsToMany method:
return $this->belongsToMany('App\Role', 'role_user');
Then it defines the inverse of the relationship :
Role model:
class Role extends Model
{
/**
* The users that belong to the role.
*/
public function users()
{
return $this->belongsToMany('App\User');
}
}
Then it says :
Since we're reusing the belongsToMany method, all of the usual table and key customization options are available when defining the inverse of many-to-many relationships.
What I understand so far:
The pivot table has been defines in User model and the docs says that
all of the usual table and key customization options are available in Role model with inverse of many-to-many relationships.
So it seems there is also the option to define the pivot table in Role model as well .
Eloquent will join the two related model names in alphabetical order to define the pivot table name by default.
On which model should I define the pivot table when the pivot table name is not according to the default convention ?