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

问题描述

我收到以下错误:

我在我的数据库中有列名称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.

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

08-04 17:00