簡體   English   中英

未定義的屬性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ name Laravel 5.3

[英]Undefined property: Illuminate\Database\Eloquent\Collection::$name Laravel 5.3

路由是Route::resource('/admin/post', 'postController');

在控制器中的代碼是

public function index()
{
    $s = Singer::where('id','<',5)->get()->name;

    return view("admin.new-post", ['s'=>$s]);
}

而且視圖是

<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>

我想從數據庫中列出所有歌手的姓名。 問題是當我在查詢中使用first()而不是get()單個值時,它將正確返回單個值,而不會出現任何錯誤。 但是,當我使用get()並在視圖中使用foreach循環獲取所有歌手時,就會出現此錯誤。 Undefined property: Illuminate\\Database\\Eloquent\\Collection::$name 我已經審查了很多次代碼,但是不明白錯誤是什么。 雖然沒有問題,但是請提供一些解決方案。

控制器代碼

public function index()
{
$s = Singer::where('id','<',5)->get();

return view("admin.new-post", ['s'=>$s]);
}

查看代碼

<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>

當您使用first()時,您將獲得一個結果,當您使用get()時,您將獲得一個結果集合 ,即使您正在搜索一個用戶。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM