Laravel: save a post with validation
Write a controller's store method that validates the request and saves a post.
intermediatePHP / Laravel· PHP & Laravel
The task
Finish PostController@store, which receives the "new post" form:
- Validate the request:
titleis required, a string, at most 120 characters;bodyis required, at least 20 characters. - Create the post with only the validated data.
- Redirect to the post's page (
posts.show) with a flash message "Post published."
If validation fails, Laravel sends the user back to the form with the errors automatically.
What you'll build
The finished result. Try it out, then build it yourself below.
Demo · drag the bottom edge to make it taller