1
votes

I have many to many related two tables, "roles" and "users" with pivot table as "user_roles".

In Role model

$this->belongsToMany('users', 'user_roles');

In User model

$this->belongsToMany('roles', 'user_roles');

Is this a valid relation?

2

2 Answers

0
votes

You want a pivot table and a many to many relationship using a pivot table like you said.

What you are trying to do is explained in Laravel documentation - Eloquent

0
votes

No. The first parameter should be the class name of the related model:

$this->belongsToMany('User', 'user_roles');

$this->belongsToMany('Role', 'user_roles');

Otherwise you should be fine if you foreign keys follow the convention...

See the documentation for more info