我是 Laravel 的新手。我正在通过表单请求进行验证。但我想设置一个条件,如果检查供应商存在返回 true,则需要检查规则,否则不需要检查。我正在制作 API,所以我希望在 else 部分有一个 JSON 响应。为此,我应该在 else 部分写什么,因为我尝试了很多方法(使用 Trait,授权功能)但给出了错误?
$check_vendor_exists = Vendor::where('user_id', Auth::id())->get()->toArray();
if (empty($check_vendor_exists)) {
return [
'business_name' => 'required||regex:/^[a-zA-Z_ ]*$/|max:191',
'business_id' => 'required|exists:businesses,id',
'business_email' => 'required|email|max:191|unique:vendors,business_email,NULL,id,deleted_at,NULL',
'country_id' => 'nullable|exists:countries,id',
'state_id' => 'nullable|exists:states,id',
'city_id' => 'nullable|exists:cities,id',
'business_logo' => 'nullable|image|mimes:jpeg,png,jpg|max:5120',
'opening_hours' => 'nullable|json',
'gst_no' => 'required||regex:/^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$/|max:191',
];
} else {
return $this->getMessages([], config('constants.messages.errors.something_wrong'), config('constants.validation_codes.unprocessable_entity'), false);
}