Property [id] does not exist on this collection instance

前端 未结 4 853
遥遥无期
遥遥无期 2021-01-04 21:44

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.

相关标签:
4条回答
  • 2021-01-04 22:00

    The error is here:

    $books->id
    

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

    @foreach ($books as $book)
        {{ $book->id }}
    @endforeach
    
    0 讨论(0)
  • 2021-01-04 22:01

    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();
    

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

    0 讨论(0)
  • 2021-01-04 22:13

    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!

    0 讨论(0)
  • 2021-01-04 22:15

    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

    foreach ($books as $book) {
         $book_id = $book->id
    }
    
    0 讨论(0)
提交回复
热议问题