Reputation: 2105
So I have this function to get the info of a user who is currently logged in.
$users = User::get();
$loginuser = $users->find(Auth::user()->id);
How do I use Laravel's eloquent ORM to get other users and put it in an array variable? Maybe something like
$otherusers = $users->find(not(Auth::user()->id));
Of course, it will not work but you get what I mean. Bonus points for improvement of the first 2 lines above~! Thank you!
Upvotes: 1
Views: 229
Reputation: 146239
You may try this to get all the users other than the logged in user:
$otherusers = User::where('id', '!=', Auth::user()->id)->get();
If you use Auth::user()
then you'll get only the currently logged in user. So for example:
// Logged In user, You can directly use
// Auth::user()->id or any property, no
// need keep it in a variable for that
$loggedInUser = Auth::user(); // Returns a single Model
// Other users (Returns a collection of user models)
// Check if a user is logged in before you use Auth::user()->id
$otherusers = User::where('id', '!=', Auth::user()->id)->get();
Upvotes: 3