栏没有找到拉扯5.4
我收到以下错误:
SQLSTATE [42S22]:未找到列:1054未知列books.id b $ b'where子句'(SQL:select * from
books
wherebooks
。id当我将id值作为id传递时,
=
98745632564 limit 1)
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'books.id' in 'where clause' (SQL: select * from
books
wherebooks
.id
= 98745632564 limit 1)
我在我的数据库中有列名称bookID,但是在上面的错误中,它正在比较books.id = 98745632564.我不明白book.id来自哪里。
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;
}
当我使用查询传递id值时,代码工作正常,如下所示
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;
}
您应该设置: p>
You should set:
protected $primaryKey = 'bookID';
在您的书籍
/ p>
in your Book
model to make:
$book = Book::findOrFail($id);
版本工作。
方法 find
或 findOrFail
正在使用主键,默认情况下设置为 id
,所以如果你有任何自定义的主键,你应该设置在你的口才模型。
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.