简体   繁体   中英

Laravel : How to use eloquent ORM to find information of NOT authenticated user?

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!

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM