Hàm Louis
Hàm Louis

Reputation: 41

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL update..) where `id` is null)

I have some code producing the following error. How can I fix it?

BookController

public function bookedit(Request $req,$id){
    $this->middleware('admin');
    $edit = Book::where('bookID','=',$id)->first();
    $edit->bookName = $req->input('bookName');
    $edit->author = $req->input('author');
    $edit->categoryID = $req->input('categoryID');
    $edit->nxb = $req->input('nxb');
    $edit->description = $req->input('description');
    $edit->price = $req->input('price');
    $edit->update();
    return redirect()->back()->withErrors(['msg', 'Sua thanh cong']);
}

Of course my primary key in Book.php is bookID not id so why do id column appear here???

Upvotes: 1

Views: 764

Answers (1)

Davit Zeynalyan
Davit Zeynalyan

Reputation: 8618

In you Book model define

class Book extends Model 
{
    protected $primaryKey = 'bookID';
    ......
}

Upvotes: 2

Related Questions