2017-09-25 46 views
-1

问题是,当我尝试更新book表,它显示错误laravel透视表列,同时尝试更新

未知列“发行”

我有一个book_publisher透视表。

我需要写什么代码以及我必须在哪写代码?

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

BookController的

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为好。

+1

你的问题不明确,没有任何代码来解释你的问题。 –

回答

0

检查这些方式

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