本文介绍了列未找到laravel 5.4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:

SQLSTATE[42S22]:未找到列:1054 Unknown column 'books.id' in'where 子句' (SQL: select * from books where books.id =98745632564 限制 1)

当我将 id 值作为 id 传递时.我的数据库中有列名 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;
}

推荐答案

你应该设置:

protected $primaryKey = 'bookID';

在你的 Book 模型中制作:

in your Book model to make:

$book = Book::findOrFail($id);

版本工作.

方法 findfindOrFail 使用主键,默认设置为 id,因此如果您有任何自定义主键,你应该在你的 Eloquent 模型中设置它.

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.

这篇关于列未找到laravel 5.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 06:34