Ajax processing of new and modified code is written in the producer, which makes the controller code bloated. Request class is used to process the verification of new and request, which makes the controller look clean and concise. The following code is modified to the version supported by Ajax, and it can be used in the background according to the conventional Baidu search.
Example code: a new doctor class has been added as an example
<? phpnamespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class DoctorPost extends FormRequest{ publicfunctionAuthorize () // This method can be used to control access permissions, such as prohibiting comments from unpaid users... {return true; / / attention! The default here isfalseRemember to change totrue } public functionRules () // This method returns an array of validation rules, that is, the Validator's validation rules {$aRules = [ 'name'= >'required|max:30'.'province'= >'required'.'city'= >'required'.'area'= >'required'.'hospital'= >'required'.'section'= >'required'.'mobile'= >'required',]; // Distinguish between additions and modifications$aData = request()->input(); if (isset($aData['code']) {$aRules['code'] = 'required'; } return $aRules; } public function messages() { $aMessages = [ 'name.required'= >'Please fill in your name'.'province.required'= >'Please select province information'.'city.required'= >'Please select city info'.'area.required'= >'Please select region information'.'hospital.required'= >'Please select hospital information'.'section.required'= >'Please select department information'.'mobile.required'= >'Please enter your mobile phone number',];$aData= request()->input(); // Distinguish between additions and modificationsif (isset($aData['code']) {$aMessages['code.required'] = 'Please enter the verification code'; } return $aMessages; } /** * the corresponding */ public when the authentication permission failsfunction forbiddenResponse() { if ($this->ajax()) { return response()->json(['msg'= >'No permissions'.'success'= >false]); } else { returnabort(403); }} /** * response when the validation parameter fails, if the specified validation rule fails, the response will be called, because errors is an array, which holds all validation error messages$errors * @return \Illuminate\Http\JsonResponse */ public function response(array $errors) { if ($this->ajax()) { $firstErrors = array_shift($errors); $error = $firstErrors[0].return response()->json(['msg'= >$error.'success'= >false]); } else { returnabort(404); }}}Copy the code
Controller call, new modification, dependency injection can be
public function updateDoc(DoctorPost $request, Doctor $oDoctor.$id) {$aData = $request->all(); unset($aData['_token']); unset($aData['mobile']); Doctor::where('id'.$id)->update($aData); return json_encode(['success'= >true]);Copy the code