I have query where i get all users with certain roles and it's working fine, what I need now is to exclude those users who has been saved in third table already and just return users without row in third table.
Logic
- Users table (
user has many roles,user hasOne manager) - Roles Table (
role belongsTo user) - Managers Table (
managers belongsTo user)
First time
User who has role of manager I return them all. (let say 2 users)
I save one of them in managers table.
Second time
User who has role of manager I return them all.
I save one of them in managers table.
Issue
In second time i should only get 1 user as I previously saved one of them into managers table. But with my current code I do still get 2 users.
Code
$users = User::whereHas("roles", function($q) {
$q->where("name", "manager");
})->get();
SQL output of code above is:
"select * from `users` where exists (select * from `roles` inner join `model_has_roles` on `roles`.`id` = `model_has_roles`.`role_id` where `users`.`id` = `model_has_roles`.`model_id` and `model_has_roles`.`model_type` = ? and `name` = ?) and `users`.`deleted_at` is null"
Any idea?
managerstable is the same as the entry inuserstable? Why is the same person in two tables? If these are separate entities, just delete from the users table when you insert into the managers table.managerhas his/her id inmanagerstable then i filter it out. that's how.