未找到列:1054 "where子句 "中的未知列 "id"(SQL: select count(*) as aggregate from `item` where `barcode` = A002 and `id` <> 12)

问题描述 投票:0回答:1

我试图更新我的表Item,但我在我的控制器中使用了独特的验证,我在我的模型中设置了primaryKey='item_id',但我一直得到这个错误说未知列ID,即使我在我的模型中设置了primaryKey为item_id,当我试图使用验证:/ 'barcode' => 'unique:item'/这个错误没有出现,程序工作,但在条形码的unique中,它正在检查自己的独特列,它失败了,但看到自己的字段。这就是为什么我把我的验证改为:/ 'barcode' => 'unique:item,barcode,'.$item_id,/当我试图更新时,它显示我这个错误。未找到列:1054 在'where子句'中未知列'id'(SQL:select count(*) as aggregate from item 哪儿 barcode = A002和 id <> 12)

ItemController。

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');
    }

这是我的modelItem.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');
    }
}
laravel validation unique
1个回答
1
投票

当你使用自定义主键时,你也应该在验证中添加它的名字。

$this->validate($request,[
           'barcode' => 'unique:item,barcode,'.$item_id.',item_id'
        ],$messages);

https:/laravel.comdocs5.2validation#rule-unique。

© www.soinside.com 2019 - 2024. All rights reserved.