Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

baskarks's avatar

laravel 11 validation request as first argument in controller method

how to pass request validation as argument in controller method like lumen in laravel

0 likes
4 replies
LaryAI's avatar
Level 58

In Laravel, you can achieve request validation directly within your controller methods by using Form Request classes. These classes encapsulate the validation logic and can be injected into your controller methods as the first argument. Here's how you can do it:

  1. Create a Form Request Class:

    You can create a Form Request class using the Artisan command:

    php artisan make:request StoreUserRequest
    
  2. Define Validation Rules:

    Open the newly created StoreUserRequest class located in app/Http/Requests/StoreUserRequest.php and define your validation rules in the rules method:

    namespace App\Http\Requests;
    
    use Illuminate\Foundation\Http\FormRequest;
    
    class StoreUserRequest extends FormRequest
    {
        public function authorize()
        {
            return true;
        }
    
        public function rules()
        {
            return [
                'name' => 'required|string|max:255',
                'email' => 'required|email|unique:users,email',
                'password' => 'required|string|min:8|confirmed',
            ];
        }
    }
    
  3. Inject the Form Request into Your Controller Method:

    In your controller, you can now type-hint the StoreUserRequest class in the method where you want to handle the request. Laravel will automatically validate the request before the method is executed:

    namespace App\Http\Controllers;
    
    use App\Http\Requests\StoreUserRequest;
    use App\Models\User;
    use Illuminate\Http\Request;
    
    class UserController extends Controller
    {
        public function store(StoreUserRequest $request)
        {
            // The incoming request is valid...
    
            // Retrieve the validated input data...
            $validated = $request->validated();
    
            // Create a new user
            $user = User::create([
                'name' => $validated['name'],
                'email' => $validated['email'],
                'password' => bcrypt($validated['password']),
            ]);
    
            return response()->json($user, 201);
        }
    }
    

By using Form Request classes, you keep your controller methods clean and focused on handling the business logic, while the validation logic is encapsulated within the Form Request class. This approach is similar to how Lumen handles request validation.

baskarks's avatar

I tried above but too many redirection issue

martinbean's avatar

how to pass request validation as argument in controller method like lumen in laravel

@baskarks You can’t. Lumen doesn’t have form requests like Laravel does. If you want Laravel features, then use Laravel.

I worked at a company where we had the exact same issue. Developers were just back-porting features from Laravel to Lumen such as form requests, route–model binding, Passport. In the end, I just said, “if we want Laravel features, why don’t we just use Laravel?” and we ended up converting the project over. It meant we could then get rid of a load of packages, and there wasn’t really any difference in loading times after the switch.

Please or to participate in this conversation.