简体   繁体   English

未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ name Laravel 5.3

[英]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.

相关问题 laravel 5.3:未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection错误 - laravel 5.3 : Undefined property: Illuminate\Database\Eloquent\Collection Error Laravel:未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ id - Laravel:Undefined property: Illuminate\Database\Eloquent\Collection::$id laravel 未定义属性:Illuminate\\Database\\Eloquent\\Collection::$id - laravel Undefined property: Illuminate\Database\Eloquent\Collection::$id Laravel 5未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ basicdetails - Laravel 5 Undefined property: Illuminate\Database\Eloquent\Collection::$basicdetails 未定义属性:Illuminate \ Database \ Eloquent \ Collection :: $ id Laravel 4 - Undefined property: Illuminate\Database\Eloquent\Collection::$id Laravel 4 Laravel 错误 leftJoin:未定义的属性:Illuminate\\Database\\Eloquent\\Collection::$id - Laravel Error leftJoin : Undefined property: Illuminate\Database\Eloquent\Collection::$id 未定义的属性:Illuminate\\Database\\Eloquent\\Collection::$likes laravel - Undefined property: Illuminate\Database\Eloquent\Collection::$likes laravel 未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: Laravel 5.2 - Undefined property: Illuminate\Database\Eloquent\Collection:: Laravel 5.2 未定义的属性:Illuminate\\Database\\Eloquent\\Relations\\BelongsTo::$name laravel 5.4 - Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name laravel 5.4 未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection - Undefined property: Illuminate\Database\Eloquent\Collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM