How would I do when querying the records of two related tables (many to many) through a Laravel pivot table?
I try to do it with Joins, but I can't find what I want. The tables are:
user
role_user
roles
How would I do when querying the records of two related tables (many to many) through a Laravel pivot table?
I try to do it with Joins, but I can't find what I want. The tables are:
user
role_user
roles
Assuming the relationships are well defined in the models, you just have to do:
The documentation explains this in detail: https://laravel.com/docs/5.3/eloquent-relationships#many-to-many
Ideally, your tables should be named
users
(with s at the end),roles
androle_user
, which should containuser_id
androle_id
.From then on, configuring the models is very simple:
Role:
Edition:
A quick example to show the users with their roles:
Controller
View
Obviously the property names change. I recommend that you read how relationships work in Laravel: https://laravel.com/docs/5.3/eloquent-relationships