Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

4lban's avatar
Level 1

Get users that doesn't relate to this workplace so I can atach them later

I want to retrieve all users that have role_id 2 and doesnt relate to workplace with id 3 so i can attach them later to this workplace with id 3.

Users with id 11 and 12 are in relationship to workplace with id 4. Users with id 13 and 14 are in relationship to wokrplace with id 3.

$users = User::where('role_id', '=', 3)->doesntHave('workplaces')->get();

this code in WorkplaceController retrieve all users that doesn't relate to any workplace, i want just users that doesn't relate with this workplace with id 3

My code:

Tables: users: id, role_id, ... . workplaces: id, ... . user_workplace (pivot): user_id, workplace_id .

User (model):

public function workplaces(){
    return $this->belongsToMany('App\Workplace');
}

Workplace (model):

public function users(){
    return $this->belongsToMany('App\User');
}   

WorkplaceController:

public function show(Workplace $workplace)
{
    $workplace = Workplace::findOrFail($workplace->id);
    $users = User::where('role_id', '=', 3)->doesntHave('workplaces')->get();   
    return view('workplaces.show', ['workplace' => $workplace, 'users' => $users]);
}
0 likes
2 replies
JordanDalton's avatar
Level 3

$users = User::where('role_id', '=', 3)->Where DoesntHave('workplaces', function($query) use($ignore_id){ $query->whereId($ignore_id);})->get();

1 like

Please or to participate in this conversation.