The [id] property does not exist on the collection instance

I am trying to create an 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 have done so far
This is mineRoute

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, the view file is of the form

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

      

Any help would be appreciated. Thanks to

+4


source to share


4 answers


You should get a single first()

non-collection entry with get()

, ie:

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

      



Please keep $books

using $book

the rest of the code.

+16


source


The error is here:

$books->id

      



When you use get()

you get a collection, and you get a $books

collection. In this case, you need to iterate over it to get its properties:

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

      

+1


source


I think your code needs to be updated, for example:

$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 job is for you!

+1


source


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


return view('backend.book.edit', compact('books'));

      

-2


source







All Articles