在驗證中,我沒有將產(chǎn)品字段名稱設(shè)置為唯一,而是希望以某種方式使其具有唯一性,例如具有相同user_id的產(chǎn)品,我希望單個用戶具有唯一的產(chǎn)品名稱。某些代碼也將產(chǎn)品字段名稱的值與數(shù)據(jù)庫中的產(chǎn)品名稱數(shù)組進行了匹配。 $products = Product::find(auth()->user()->id)->get(); foreach ($products as $product) { $pro_name = $product->name; } $value = Input::get('name'); if ($value == anyOf($pro_name) { return false; }
2 回答

慕碼人8056858
TA貢獻1803條經(jīng)驗 獲得超6個贊
也可以使用標準Laravel驗證來定義此驗證:
$this->validate(
$request,
[
'name' => \Illuminate\Validation\Rule::unique('products', 'name')
->ignore($id) // id of product being updated. If it is a new product you can remove this, or pass null
->where(function ($query) {
return $query->where('user_id', \Auth::user()->id);
})
]
);
- 2 回答
- 0 關(guān)注
- 207 瀏覽
添加回答
舉報
0/150
提交
取消