php 在laravel eloquent中获取除当前登录用户之外的所有用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24613565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:24:14  来源:igfitidea点击:

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

phpmysqllaravel

提问by Priyanka

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!

提前致谢!

回答by damiani

Using except()will accomplish the same thing a little more fluently:

使用except()将更流畅地完成同样的事情:

$users = User::all()->except(Auth::id());

...or, since you already have the User id:

...或者,因为您已经拥有用户 ID:

$users = User::all()->except($currentUser->id);

回答by Joseph Silber

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

您可以使用auth()->id(). 然后将其传递给查询:

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

回答by Lucas Ferreira

If you are using the Auth helper, use this.

如果您正在使用 Auth 助手,请使用它。

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

回答by Pils

Use Auth;
Use App\User;

public function index()
$id = Auth::id();
$result = User::where('id', '!=', $id)->get();
dd($result);