【发布时间】:2019-02-20 21:45:52
【问题描述】:
基本上我只想转储所有具有特定角色的用户。当我这样做时:
$users = User::with('roles')->get();
我将所有用户和角色添加到用户上,如下所示:
{
id: 1,
first_name: "P",
last_name: "Tag",
email: "packy@test.com",
phone: "5555555555",
avatar: "",
last_login: "2016-07-13 23:49:23",
created_at: "2016-07-13 23:25:05",
updated_at: "2016-07-13 23:49:23",
roles: [
{
id: 1,
name: "owner",
display_name: "Admin",
description: "Admin user that has full access",
created_at: "2016-06-07 00:00:00",
updated_at: "2016-06-07 00:00:00",
pivot: {
user_id: 1,
role_id: 1
}
}
]
},
现在太好了,我只想获取具有特定值的角色名称的用户。我想我的查询看起来像这样,但不知道怎么写:
$users = User::with('roles')->where('roles->name', '=', 'owner')->get();
但这当然行不通。
【问题讨论】: