2017-04-10 24 views
3

我想創建編輯頁面,但此錯誤不斷彈出 Whoops, looks like something went wrong. Property [id] does not exist on this collection instance. 我所做到目前爲止
這是我Route地產[ID]在此集合實例不存在

Route::get('book/edit/{id}', '[email protected]')->name('admin.book.edit'); 
Route::PATCH('book/edit/{id}', '[email protected]')->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)); 

最後查看的文件有以下的組成部分

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

任何幫助將不勝感激。由於

+0

的可能的複製[Laravel - 物業\ [標題\]不會對這種集合實例存在(http://stackoverflow.com/questions/41366092/laravel-property-title-does-not-exist -on-this-collection-instance) – Thamaraiselvam

回答

7

你必須獲取一個紀錄first()不是一個集合與get(),即:

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

請sobstitute $books$book在其餘的代碼也。

0

的錯誤是在這裏:

$books->id 

當你使用get()你會得到一個收集和$books是一個集合。在這種情況下,你需要遍歷它來獲得屬性:

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

我覺得你的代碼需要像更新:

$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')); 

希望這對你的工作!

-2
$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')); 
+0

我很肯定這並不能解決問題。請添加一些細節來解釋您的答案,謝謝。 –

相關問題