Laravel 5 尝试更新时未找到laravel pivot表列
问题是,当我尝试更新Laravel 5 尝试更新时未找到laravel pivot表列,laravel-5,pivot,Laravel 5,Pivot,问题是,当我尝试更新图书表时,它显示错误 未知列“publisher” 我有一个book\u出版商pivot表 我必须写什么代码?我必须在哪里写 class Book extends Model { // protected table='book'; public function authors() { return $this->belongsToMany(Author::class)->withPivot('type'); }
图书
表时,它显示错误
未知列“publisher”
我有一个book\u出版商
pivot表
我必须写什么代码?我必须在哪里写
class Book extends Model
{
// protected table='book';
public function authors()
{
return $this->belongsToMany(Author::class)->withPivot('type');
}
// public function author($type)
// {
//
// return $this->authors()->where('type', $type)->first();
// }
public function author()
{
return $this->authors()->where('type', 'Author')->first();
}
public function illustrator()
{
return $this->authors()->where('type', 'Illustrator')->first();
}
public function translator()
{
return $this->authors()->where('type', 'Translator')->first();
}
public function editor()
{
return $this->authors()->where('type', 'Editor')->first();
}
// foreach ($book->authors as $author)
// {
// $author->type;
// }
public function publishers()
{
return $this->belongsToMany(Publisher::class);
}
}
图书管理员
public function edit($id)
{
$book=Book::findOrFail($id);
$category=Category::pluck('name', 'id');
$publishers=Publisher::all();
foreach($publishers as $publisher)
{
$publisher->id=$publisher->name;
}
return view('books.edit', compact('book','category','publishers'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$book=Book::findOrFail($id);
$book->Update($request->all());
Book::find($id)->publisher()->updateExistingPivot($request->only('publisher_id'));
return redirect('books');
}
还请告诉我是否需要编辑edit.blade.php。检查这些方法
$books = Book::find($id);
$books->publisher()->updateExistingPivot('publisher_id',
['publisher_id' => publisher_id],false));
或者类似的事情
$books = Book::find($id);
$publisher = $books->publisher;
$publisher->pivot->publisher_id = $request->publisher_id;
$publisher->pivot->save();
您的问题不清楚,并且缺少任何代码来解释您的问题。