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

ajay01's avatar

QueryException General error: 1096 No tables used

I am trying to get all users but the condition is that they must have a valid role, the selected user should not be logged in user and the third condition is in whereNotIn clauses closure function. every thing else is working fine but for whereNotIn clauses, I'm getting an error like 1096 No tables used. I don't know what's wrong I'm doing.

here, is my code snippet.

                $allUsers = User::orderBy('users.created_at', 'asc')
                            ->join('role_users', 'users.id', '=', 'role_users.user_id')
                            ->join('roles', 'role_users.role_id', '=', 'roles.id')
                            ->where('role_users.role_id', '=', '3')
                            ->where('users.id' , '!=', $userData['user_id'])
                            ->whereNotIn('users.id', function($query) use ($addressId) {
                                $userId = PAddressDetail::find($addressId)->colistAgent()->get()->pluck('id')->toArray();
                            })->get()->toArray();
                dd($allUsers);

Here is the screenshot of error page

0 likes
3 replies
MichalOravec's avatar
Level 75
$ids = PAddressDetail::find($addressId)->colistAgent()->pluck('id');

$allUsers = User::orderBy('users.created_at', 'asc')
    ->join('role_users', 'users.id', '=', 'role_users.user_id')
    ->join('roles', 'role_users.role_id', '=', 'roles.id')
    ->where('role_users.role_id', '=', '3')
    ->where('users.id' , '!=', $userData['user_id'])
    ->whereNotIn('users.id', $ids)
    ->get()
    ->toArray();
1 like
ajay01's avatar

Thanks, Michal. I'm already using this solution right now. Can you please explain why we can't use the relationship sub-query in the closure function?

MichalOravec's avatar

I think you just forget to add return in the closure.

1 like

Please or to participate in this conversation.