laravel 创建request表单提交验证类
php artisan make:request Community/CreateOrUpdateRequest
<?php namespace App\Http\Requests\Community; use Illuminate\Foundation\Http\FormRequest; class CreateOrUpdateRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'community_name' => 'required|max:255', 'community_code' => 'max:255', 'address' => 'required:max:255', 'households' => 'required|numeric', 'containernum' => 'numeric', 'contact' => 'required:max:255', 'contactinfo' => 'required:max:255', ]; } public function messages() { //return parent::messages(); // TODO: Change the autogenerated stub return [ 'community_name.required' => '请填写小区名称', 'address.required' => '请填写小区地址' ]; } }
//逻辑控制器update 方法 里面的CreateOrUpdateRequest
<?php namespace App\Http\Controllers\Admin; use App\Http\Requests\Community\CreateOrUpdateRequest; use App\Models\Community; use Illuminate\Http\Request; use App\Http\Controllers\Controller; class CommunityController extends Controller { // public function index(Request $request) { $communities = Community::query()->where(request_intersect(['name']))->paginate($request->get("limit")); return view("admin.community.index", compact("communities")); } /** * * @param $id * @return PermissionResource */ public function show($id) { return new CommunityResource(Community::query()->findOrFail($id)); } /** * @param Permission $permission * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function edit(Community $community) { return view("admin.community.edit", compact("community")); } /** * * @param CreateOrUpdateRequest $request * @param $id * @return \Illuminate\Http\JsonResponse */ public function update(CreateOrUpdateRequest $request, $id) { $community = Community::query()->findOrFail($id); $community->update(request_intersect([ 'community_name', 'community_code', 'address' ])); return $this->success(); } }