Support the ongoing development of Laravel.io →
Input Database Eloquent

Hello.

I am trying to get all users that are not currently assigned to a location in a many to many relationship.

I have no clue on how I could aproach this.

Currently I am getting only the users that have no location at all. But I need to use the $location_id to filter the result.

    public function getUnassignedUsersForLocation($location_id)
    {
        $users = User::select(['id', 'name'])
            ->has('locations', '=', 0)
            ->get();

        return response()->json($users);
    }

Any possible solutions?

Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

uibar uibar Joined 20 Mar 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.