1

在laravel 表单验证中,常会遇到需要几个字段组合起来做唯一限制。
解决方案如下:

$where[] = ['parentId','=',$this->request->get('parentId')];

return [

        'menuTitle' => ['required', 'max:32','min:2',Rule::unique('admin_menu','menuTitle')->where(function($query)use($where){
            $query->where($where)->whereNull('deleted_at');
            })->ignore($id) ],
        'menuTitleEn' => ['required', 'max:32','min:2',Rule::unique('admin_menu','menuTitleEn')->where(function($query)use($where){
            $query->where($where)->whereNull('deleted_at');
            })->ignore($id) ],
        'menuRoute' => ['required',Rule::unique('admin_menu','menuRoute')->ignore($id)],
        'menuIcon' => ['required', 'min:2','max:32'],
        'routeName' => ['sometimes', 'min:2','max:32'],
        'parentId' => ['required','numeric'],
        'order'=>['sometimes','numeric']
        
    ];

末路伯爵
13 声望1 粉丝

只会写bug