laravel - 未找到列:1054 'where 子句'中的未知列 'id'(SQL:从 `item` 中选择 count(*) 作为聚合,其中 `barcode` = A002 和 `id` <> 12)
问题描述
我尝试更新我的表Item
,但我在我的控制器中使用了唯一验证,我在我的模型中设置了主键 = 'item_id' 但是我一直收到这个错误说未知的列 id,即使我已经将主键设置为我的 item_id模型,当我尝试使用验证时:
// 'barcode' => 'unique:item' //
此错误不会出现并且程序可以工作,但是在唯一的条形码中,它正在检查自己的唯一列并且它失败但看到了自己的字段。这就是为什么我将验证更改为:
// 'barcode' => 'unique:item,barcode,'.$item_id, //
当我尝试更新时出现此错误:
找不到列:1054 'where 子句'中的未知列 'id'(SQL:从
item
wherebarcode
= A002 和id
<> 12 中选择 count(*) 作为聚合)
项目控制器:
public function update(Request $request,$item_id)
{
$messages = [
'unique' => 'Sorry this barcode already exist....',
];
$this->validate($request,[
'barcode' => 'unique:item,barcode,'.$item_id,
],$messages);
$data = $request->except(['_token']);
Item::where('item_id',$item_id)->update($data);
return redirect('/item')->with('sukses','item successfully updated');
}
这是我的模型 Item.php :
class Item extends Model
{
protected $table = 'item';
protected $primaryKey = 'item_id';
protected $fillable = ['barcode','item_name','category_id','satuan_id','price'];
public function category_r(){
return $this->belongsTo('App\Category','category_id','category_id');
}
public function satuan_r(){
return $this->belongsTo('App\Satuan','satuan_id','satuan_id');
}
}
解决方案
当您使用自定义主键时,您还应该将其名称添加到验证中:
$this->validate($request,[
'barcode' => 'unique:item,barcode,'.$item_id.',item_id'
],$messages);
推荐阅读
- node.js - 开玩笑地编写流式测试用例以从远程 url 下载
- android - 系统级别控制的 Android 通知打开或关闭
- gradle - Gradle:在构建时和运行时运行代码
- android-studio - Android Studio 启动失败,没有任何错误信息
- image - 当我尝试在 Symfony 3 上获取图像时出现 CORS 错误
- php - Laravel 6 - 如何使用数组和输入的数据重定向回来?
- python - 停止运行 python 程序的 systemd 服务
- python - 枚举类定义过程
- python - 如何使用谷歌服务帐户设置谷歌应用脚本网络应用端点授权?
- c# - For Each Row:集合发生变化,可能无法进行枚举操作