没有 id 的 Laravel 5 更新

Org*_*ila 5 php laravel laravel-5

我在 Laravel 5 中遇到问题。当我更新数据库中的记录时,出现错误Unknown column 'id' in 'where clause'。在控制器中,我使用 WHERE 子句来获取我想要更新的记录。我的主键不是 id,类型是 bigInteger。我试过添加protected $primaryKey模型,但它不适用于 bigInteger。有什么办法可以使用我自己的主键而不是使用 id 吗?

控制器

$item = Item::where('item_id','=',$item_id)->first();
$item.name = $name;
$item.type = $type;
$item.save();
Run Code Online (Sandbox Code Playgroud)

小智 3

请将这一行添加到您的 Item.php 模型中

class Item extends Model {


// 只添加这一行
protected $primaryKey = 'item_id';

//..... the rest of your model

由于您使用自定义 id 名称,如果不指定,laravel 将不知道您的主键是什么