[英]Undefined property: Illuminate\Database\Eloquent\Collection::$name Laravel 5.3
The Route is Route::resource('/admin/post', 'postController');
路由是Route::resource('/admin/post', 'postController');
in controller the code is 在控制器中的代码是
public function index()
{
$s = Singer::where('id','<',5)->get()->name;
return view("admin.new-post", ['s'=>$s]);
}
And the view is 而且视图是
<select class="form-control select2 select2-hidden-accessible" multiple="" data-placeholder="Select a State" style="width: 100%;" tabindex="-1" aria-hidden="true">
<?php foreach ($s as $ss){
echo '<option>'. $ss .'</option>';
}?>
</select>
I want to list down all the names of singers from data base in select. 我想从数据库中列出所有歌手的姓名。 And the problem is that when I get a single value using first()
in query instead of get()
it returns single value correctly widhout any error. 问题是当我在查询中使用first()
而不是get()
单个值时,它将正确返回单个值,而不会出现任何错误。 but when I use get()
and in view use foreach
loop to get all singers then it gives this error. 但是,当我使用get()
并在视图中使用foreach
循环获取所有歌手时,就会出现此错误。 Undefined property: Illuminate\\Database\\Eloquent\\Collection::$name
. Undefined property: Illuminate\\Database\\Eloquent\\Collection::$name
。 I have reviewed code many times but could not understand that what the error is. 我已经审查了很多次代码,但是不明白错误是什么。 while there is no problem to get a single name.. please provide me some solution. 虽然没有问题,但是请提供一些解决方案。
Controller Code 控制器代码
public function index()
{
$s = Singer::where('id','<',5)->get();
return view("admin.new-post", ['s'=>$s]);
}
View Code 查看代码
<select class="form-control select2 select2-hidden-accessible" multiple="" data-placeholder="Select a State" style="width: 100%;" tabindex="-1" aria-hidden="true">
<?php foreach ($s as $ss){
echo '<option>'. $ss->name .'</option>';
}?>
</select>
When you use first() you get a single result, when you use get() you are getting a collection of results, even if your searching for one user. 当您使用first()时,您将获得一个结果,当您使用get()时,您将获得一个结果集合 ,即使您正在搜索一个用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.