Using Forms and Gathering Input in Laravel
Building interactive web applications means accepting and processing user input — and Laravel makes handling forms secure, simple, and powerful.
In this post, you’ll learn:
-
How to create HTML forms in Laravel views
-
Handling form submission in controllers
-
Validating user input
-
Using old input and error messages
-
Protecting forms with CSRF tokens
️ Creating a Basic Form in Blade
Here’s a simple example of a form that collects a user’s name and email:
-
@csrfadds a Cross-Site Request Forgery token to protect your form -
old('field')repopulates the input after validation errors
Setting Up the Route
In your routes/web.php:
Handling Form Submission in Controller
In UserController.php:
⚠️ Validation and Error Handling
Laravel’s $request->validate() automatically redirects back with errors if validation fails.
Display errors in your Blade view:
✅ Best Practices
-
Always use
@csrfto protect forms -
Validate user input rigorously
-
Use
old()to keep input after errors -
Display user-friendly error messages
-
Sanitize and escape output when displaying data
Wrapping Up
Handling forms and user input is essential for any Laravel app. By using Blade forms, controllers, and built-in validation, you ensure your app is secure, robust, and user-friendly.
