问题
I get the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'books.id' in 'where clause' (SQL: select * from
books
wherebooks
.id
= 98745632564 limit 1)
when I pass id value as id. I have column name bookID in my database but in the above error it is comparing books.id = 98745632564. I could not understand where book.id is coming from.
public function showBook($id){
$book = Book::findOrFail($id);
return $book;
}
The code works perfectly fine when I pass id value with the query as follows
public function showBook($id){
$book = Book::where('bookID', $id)->find();
return $book;
}
回答1:
You should set:
protected $primaryKey = 'bookID';
in your Book
model to make:
$book = Book::findOrFail($id);
version work.
Methods find
or findOrFail
are using primary key and this is by default set to id
, so if you have any custom primary key, you should set it in your Eloquent model.
来源:https://stackoverflow.com/questions/43478429/column-not-found-laravel-5-4