Laravel:如何使用 eloquent ORM 查找未经过身份验证的用户的信息?

Posted

技术标签:

【中文标题】Laravel:如何使用 eloquent ORM 查找未经过身份验证的用户的信息?【英文标题】:Laravel : How to use eloquent ORM to find information of NOT authenticated user? 【发布时间】:2014-08-03 11:42:40 【问题描述】:

所以我有这个功能来获取当前登录用户的信息。

$users = User::get();
$loginuser = $users->find(Auth::user()->id);

如何使用 Laravel 雄辩的 ORM 获取其他用户并将其放入数组变量中?也许像

$otherusers = $users->find(not(Auth::user()->id));

当然,它会不起作用,但你明白我的意思。上面前2行的改进加分~!谢谢!

【问题讨论】:

【参考方案1】:

你可以试试这个来获取除登录用户之外的所有用户:

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

如果您使用Auth::user(),那么您将只获得当前登录的用户。比如:

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

【讨论】:

谢谢!欣赏它:) +1 只需确保确实有用户登录。否则您将收到此代码错误。

以上是关于Laravel:如何使用 eloquent ORM 查找未经过身份验证的用户的信息?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Eloquent ORM laravel 中获取最后一个插入 ID

如何在 Laravel Eloquent ORM 中获取插入的多行数据

如何在不使用Eloquent ORM时构建Laravel应用程序

laravel Eloquent ORM - 如何获得编译查询?

Angular JS 与 Laravel - 如何绑定 Eloquent ORM 数组以查看

如何根据 laravel eloquent orm 中的两列进行查询