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!
get() is used when you want to add queries and all() is used to get all data, without using any condition. Example of all(): $query = Project::all(); Example of get(): $query = Project::select('id', 'name')->where('name', '')->orderBy('id', 'desc')->get(); Follow this answer to receive notifications.
The except method will return all the key/value pairs in the collection where the keys in the collection are not in the supplied $keys array. Internally, this method makes a call to the "Illuminate\Support\Arr:except($array, $keys)" helper function.
Using except()
will accomplish the same thing a little more fluently:
$users = User::all()->except(Auth::id());
...or, since you already have the User id:
$users = User::all()->except($currentUser->id);
You can get the current user's id with auth()->id()
. Then pass that to the query:
$users = User::where('id', '!=', auth()->id())->get();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With