Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
Retrieve all models that are not associated with another model through pivot

I have three tables: users, organizations, organization_user. organization_user is a pivot table for the many-to-many relationship between users and organizations. The many-to-many relationships have been setup correctly in the corresponding models.

I need to obtain all users who are not associated with a given organization. How should this be done using eloquent. Below is what I have tried, but it is returning no results:

public function associate_user($organization_id){

        $data = [
            'organization'      => \App\Organization::find($organization_id),
            'users'             => \App\User::whereDoesntHave('organizations', function($query) use ($organization_id){
                $query->where('organization_id', $organization_id);
            })
        ];

        return view('admin.associateUser', $data);

    }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You are never actually executing the query.

You need to call get() at the end of the query builder.

public function associate_user($organization_id) {
    $data = [
        'organization' => \App\Organization::find($organization_id),
        'users'        => \App\User::whereDoesntHave('organizations', function($query) use ($organization_id){
            $query->where('organization_id', $organization_id);
        })->get(); // Add the call to get()
    ];
    return view('admin.associateUser', data);
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!