我在 Laravel 5 中遇到问题。当我更新数据库中的记录时,出现错误 Unknown column 'id' in 'where clause'
。在 Controller 中,我使用 WHERE 子句来获取我要更新的记录。我的主键不是 id,类型是 bigInteger。我试过在模型中添加 protected $primaryKey
但它不适用于 bigInteger。有没有办法使用我自己的主键而不是使用 id?
Controller
$item = Item::where('item_id','=',$item_id)->first();
$item.name = $name;
$item.type = $type;
$item.save();
最佳答案
请将此行添加到您的 Item.php 模型
类 Item 扩展模型 {
//只添加这一行
protected $primaryKey = 'item_id';
//.....你模型的其余部分
因为你使用自定义 id 名称,如果你不指定它,laravel 将不知道你的主键是什么
关于php - 没有 id 的 Laravel 5 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36270417/