Validated Data from Laravel Form Request
Published in
1 min readSep 22, 2017
There are several ways to validate a request in Laravel. Recently, Laravel 5.5 was released with a few improvements on request validation. Like validate
method on Controller now returns validated data, which is covered in laravel-news.
So, I am a big fan of Laravel’s Form request. Now, how can I get the validated data in Form Request ?
Let’s say I have a PostRequest
as below:
class PostRequest extends FormRequest { public function rules()
{
return [
'title' => 'required',
'content' => 'required',
];
}
}
Now, let’s validate request using the above PostRequest.
// on your post controllerpublic function store(PostRequest $request)
{
$data = $request->validated(); // here is you magic method return Post::create($data);
}
As you can see, it can be pretty handy if you have a big form and calling $request->only(...)
can be hectic.
Happy Coding…