【问题标题】:Column not found laravel 5.4未找到列laravel 5.4
【发布时间】:2021-04-07 21:42:34
【问题描述】:

我收到以下错误:

SQLSTATE[42S22]:未找到列:1054 中的未知列“books.id” 'where 子句' (SQL: select * from books where books.id = 98745632564 限制 1)

当我将 id 值作为 id 传递时。我的数据库中有列名 bookID,但在上述错误中,它比较的是 books.id = 98745632564。我不明白 book.id 的来源。

public function showBook($id){
   $book = Book::findOrFail($id);
   return $book;
}

当我通过如下查询传递 id 值时,代码工作得非常好

public function showBook($id){
    $book = Book::where('bookID', $id)->find();
    return $book;
}

【问题讨论】:

    标签: laravel laravel-5 eloquent laravel-5.4


    【解决方案1】:

    你应该设置:

    protected $primaryKey = 'bookID';
    

    在您的Book 模型中制作:

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

    版本工作。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-15
      • 2017-09-19
      • 2018-12-23
      • 1970-01-01
      • 2017-09-14
      • 2017-09-11
      相关资源
      最近更新 更多