该集合实例上不存在属性[id] [英] Property [id] does not exist on this collection instance

查看:135
本文介绍了该集合实例上不存在属性[id]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建编辑页面,并且此错误不断弹出 Whoops, looks like something went wrong. Property [id] does not exist on this collection instance. 到目前为止,我已经完成了什么
这是我的Route

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

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

这是我的控制器

$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

推荐答案

您必须使用first()而不是get()的集合来检索一条记录,即:

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

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

请在其余的代码中用$book吸收$books.

Please sobstitute $books with $book in the rest of the code also.

这篇关于该集合实例上不存在属性[id]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆