php 此集合实例上不存在属性 [id]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43320223/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:48:42  来源:igfitidea点击:

Property [id] does not exist on this collection instance

phplaravel

提问by Regolith

I am trying to create edit page and this error keeps popping up Whoops, looks like something went wrong. Property [id] does not exist on this collection instance.What I've done so far
This is my Route

我正在尝试创建编辑页面,但此错误不断弹出 Whoops, looks like something went wrong. Property [id] does not exist on this collection instance.到目前为止我所做的事情
这是我的Route

Route::get('book/edit/{id}', 'BookController@edit')->name('admin.book.edit');
Route::PATCH('book/edit/{id}', 'BookController@update')->name('admin.book.edit');

This is my controller

这是我的控制器

$books = $this->bookModel
        ->join('author', 'author.id', '=', 'book.author_id')
        ->where('book.id', '=', $id)
        ->select('book.*', 'author.name_1 as authorname1')
        ->get();
    return view('backend.book.edit', compact('books', $books));

Finally view file have the following in form part

最后查看文件在表单部分有以下内容

{{ Form::model($books, ['route' => ['admin.book.edit', $books->id], 'class' => 'form-horizontal', 'role' => 'form', 'method' => 'PATCH']) }}
<!--form content-->
{{ Form::close() }}

Any help will be appreciated. Thanks

任何帮助将不胜感激。谢谢

回答by dparoli

You have to retrieve one record with first()not a collection with get(), i.e:

您必须检索一个first()没有集合的记录get(),即:

$book = $this->bookModel
    ->join('author', 'author.id', '=', 'book.author_id')
    ->where('book.id', '=', $id)
    ->select('book.*', 'author.name_1 as authorname1')
    ->first();

Please sobstitute $bookswith $bookin the rest of the code also.

请sobstitute$books$book代码也休息。

回答by Mayank Pandeyz

The error is here:

错误在这里:

$books->id

When you're using get()you get a collection and $booksis a collection. In this case you need to iterate over it to get its properties:

当您使用时,您将get()获得一个集合并且$books是一个集合。在这种情况下,您需要迭代它以获取其属性:

@foreach ($books as $book)
    {{ $book->id }}
@endforeach

回答by AddWeb Solution Pvt Ltd

I think your code need to update like:

我认为您的代码需要更新如下:

$books = $this->bookModel
       ->join('author', 'author.id', '=', 'book.author_id')
       ->where('book.id', '=', $id)
       ->select('book.*', 'author.name_1 as authorname1')
       ->first();
   return view('backend.book.edit', compact('books'));

Hope this work for you!

希望这对你有用!

回答by DragonFire

As Mayank mentioned above you get a collection, you neeed to itreate over the FIELDS to get the right field you want. I am giving the same answer in the sytax which is working for me

正如 Mayank 上面提到的,你得到一个集合,你需要遍历 FIELDS 以获得你想要的正确字段。我在对我有用的语法中给出了相同的答案

foreach ($books as $book) {
     $book_id = $book->id
}