For anything beyond a couple of rules, extracting validation into a dedicated Form Request class keeps controllers thin and rules reusable.
php artisan make:request StorePostRequest
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StorePostRequest extends FormRequest
{
public function authorize(): bool
{
return $this->user()->can('create', Post::class);
}
public function rules(): array
{
return [
'title' => 'required|string|max:255',
'body' => 'required|string|min:10',
];
}
public function messages(): array
{
return [
'title.required' => 'Please give your post a title.',
];
}
}
Then simply type-hint it in the controller — Laravel automatically validates the request before the method body even runs, and returns a redirect with errors if it fails:
public function store(StorePostRequest $request)
{
Post::create($request->validated());
return redirect('/posts');
}
The authorize() method lets you combine validation with authorization in one place — returning false triggers a 403 response automatically, before validation even runs.