Laravel:此集合实例上不存在属性 [email]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47201250/
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
Laravel: Property [email] does not exist on this collection instance
提问by horcrux88
Hi I keep getting an error saying property [email] does not exist on this collection instance. What am I doing wrong? What is the correct way to retrieve the email address?
嗨,我不断收到错误消息,指出此集合实例上不存在属性 [email]。我究竟做错了什么?检索电子邮件地址的正确方法是什么?
Here is my code in controller:
这是我在控制器中的代码:
public function runCommand(Request $request){
$user = User::select("id", "email")->get();
$signature = $request->input('signature');
$command = Artisan::call($signature, ['user' => $user->email]);
return response($command);
}
回答by Jerodev
You are selecting all users from the database. And a collection of users does not have a single property email
. You should add a filter and use the first()
function to get a single user object from the database.
您正在从数据库中选择所有用户。并且用户集合没有单一属性email
。您应该添加一个过滤器并使用该first()
函数从数据库中获取单个用户对象。
Like so:
像这样:
$user = User::where('id', 1)->select("id", "email")->first();
dd($user->email);
回答by Vahe Shak
get()
method returns collection, use first()
.
get()
方法返回集合,使用 first()
.
$user = User::where('id', $id)->select("id", "email")->first();