Use Laravel's excellent FormRequest as Singleton
- run
composer require lextira/laravel-formrequest-singleton - open
config/app.phpin your project - replace
Illuminate\Foundation\Providers\FoundationServiceProvider::class
withLextira\FormRequestSingleton\FoundationServiceProvider::class - done!
All classes, which extend Illuminate\Foundation\Http\FormRequest are now instantiated as singleton.
This brings the following benefits:
- Changes done to the request by
prepareForValidation()are applied only once, even if the FormRequest is used multiple times. - The request validation is run only once, therefore especially database queries run only once.