Priyanka Priyanka - 1 month ago 18
MySQL Question

Get all the users except current logged in user in laravel eloquent

I am doing:

User::all();


to get all the users from users table. I want to select all the users except current logged in user. How should I do that? something like,

User::where('id','!=',$currentUser->id)->get();


Thanks in advance!

Answer

You can get the current user's id with Auth::id(). Then pass that to the query:

$users = User::where('id', '!=', Auth::id())->get();