https://dev.to/thiagomrvieira #the-scenario The scenario It’s not rare to start working on a project and need to deal with god classes controllers.
Who never faced a controller store method that validates, persists, creates a relation, sends emails, …
I’ll just authorize the use and cut my validation rules from controller and paste it in the rules method like this: 'required', 'email' => 'required|unique:users', 'phone_number' => 'required', ]; }} https://laravel.com/docs/9.x/validation this class: All you need to do is type-hint the request on your controller method. The incoming form request is validated before the controller method is called, meaning you do not need to clutter your controller with any validation logic use App\Http\Requests\StoreUserRequest; public function store(StoreUserRequest $request) { # Create a user $user = User::create([ 'name' => $request->name, 'email' => $request->email, 'phone_number' => $request->phone_number, ]); # Create the user's roles if(!empty($request->roles)) { $user->roles()->sync($request->roles); }