I have three tables as below:
id|name|username|password
id|name
id|user_id|role_id
These tables communicate via belongsToMany. I would like to find a way to select all data in “users” table except ones that their user value of "role_id" is 5 in table “users_roles”. How can I do it?
You should use whereDoesntHave() to select models that don't have a related model meeting certain criteria:
$users = User::whereDoesntHave('roles', function($q){
$q->where('role_id', 5);
})->get();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With