Check if name is unique among non-deleted items with laravel validation

后端 未结 5 752
走了就别回头了
走了就别回头了 2020-12-05 14:06

I have a simple form which posts to a controller which checks if a name for an item is already taken for a particular project. If it is, then it returns an error. This is th

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 14:26

    For add record

    'name' => [
                    'required',
                    'string',
                    'min:3',
                    Rule::unique('products')->where(function ($query) {
                        return $query->where('store_id', Auth::user()->store_id);
                    })->whereNull('deleted_at'),                
                ],
    

    For edit that record

    'name' => [
                    'required',
                    'string',
                    'min:3',
                    Rule::unique('products')->where(function ($query) {
                        return $query->where('store_id', Auth::user()->store_id);
                    })->ignore($request->id, 'id')->whereNull('deleted_at'),                
                ],
    

提交回复
热议问题