Learn how to build HTML forms and use the Request object to handle user input in your Laravel application. Master input handling for your project.
Previously in this course, we learned how to display database records in our views. Now that our application can show data, it needs a way to let users add new tasks. In this lesson, we will master the fundamentals of capturing user input using HTML forms and the Laravel Request object.
Every time a user visits a URL or submits a form, they send a "request" to your server. In Laravel, this isn't just a global array like $_POST; it is a powerful, object-oriented wrapper.
The Illuminate\Http\Request object acts as the single source of truth for everything the user sent. Whether it's form fields, query parameters in the URL, or uploaded files, the request object provides a clean, unified API to access that data. Instead of digging through PHP superglobals, you'll inject this object into your controller methods.
First, let's create the view where our user will input their task. In your resources/views/tasks/create.blade.php file, create a standard HTML form.
HTMLstyle="color:#808080"><style="color:#4EC9B0">form action="/tasks" method="POST"> <!-- We will cover this directive in a future lesson --> @csrf style="color:#808080"><style="color:#4EC9B0">label for="title">Task Title:style="color:#808080"></style="color:#4EC9B0">label> style="color:#808080"><style="color:#4EC9B0">input type="text" name="title" id="title"> style="color:#808080"><style="color:#4EC9B0">label for="description">Description:style="color:#808080"></style="color:#4EC9B0">label> style="color:#808080"><style="color:#4EC9B0">textarea name="description" id="description">style="color:#808080"></style="color:#4EC9B0">textarea> style="color:#808080"><style="color:#4EC9B0">button type="submit">Add Taskstyle="color:#808080"></style="color:#4EC9B0">button> style="color:#808080"></style="color:#4EC9B0">form>
A few things to note here:
action attribute: This tells the browser where to send the data. We'll point this to our /tasks route.method attribute: Always use POST for creating data.name attributes: These are critical. The name attribute in your HTML input becomes the key you will use to retrieve the data in your controller.Now, we need to capture that data. In your TasksController, update your store method to accept the Request object as an argument. Laravel’s service container will automatically "inject" the current request into this method.
PHPnamespace App\Http\Controllers; use Illuminate\Http\Request; class TasksController extends Controller { public function store(Request $request) { #6A9955">// Retrieve all input as an array $input = $request->all(); #6A9955">// Retrieve a single piece of input by name $title = $request->input('title'); #6A9955">// Or use the shorthand property access $description = $request->title; #6A9955">// For now, let's just dump the data to verify it works dd($request->all()); } }
The dd() function stands for "Dump and Die." It’s a developer's best friend for inspecting the contents of the request object. If you submit the form, you’ll see an array containing your title and description.
It is time to advance our Task Manager project. Follow these steps:
POST route to routes/web.php that points to the store method of your TasksController.create.blade.php has the correct action and method attributes.TasksController@store, use the $request object to capture the input and return a simple string, like "Task received!", instead of dd().Even experienced developers trip over these basics. Here is how to avoid them:
name attributes: If you forget to add a name attribute to your <input> tags, the browser won't send that data to the server. Your request object will simply ignore it.Route::get('/tasks', ...) but your form uses method="POST", you will encounter a 405 Method Not Allowed error. Always match your route definition to your form method.$request->input('title') before the user has submitted anything. Remember that this data only exists after the form is submitted to the controller action.We've moved from static pages to interactive applications. By using the Request object, we can now safely access user input submitted through HTML forms. This input handling is the foundation for creating, updating, and managing data in any web application. You’ve successfully bridged the gap between the frontend UI and your backend controller logic.
Up next: Now that we can capture user input, we need to make sure it's actually valid before saving it to our database. We will cover that in our lesson on Introduction to Laravel Validation.
Learn to customize Laravel validation error messages to provide clear, helpful feedback to your users. Master field-specific errors and language files.
Read moreLearn how to use Laravel validation to ensure data integrity. Discover how to apply rules, handle failures, and display error messages in your Blade views.
Capturing User Input from Forms
Protecting Routes with Middleware
Understanding CSRF Protection
Preventing Mass Assignment
Task Manager: Securing the Application
Introduction to Route Model Binding
Updating Existing Records
Deleting Records
Using Named Routes
Task Manager: Completing CRUD Functionality
Introduction to Database Relationships
Querying Related Data
Handling File Uploads
Using Flash Messages for User Feedback
Task Manager: Adding Status and Priorities
Introduction to Artisan Commands
Debugging with Laravel Tinker
Understanding Service Providers
Using View Composers
Task Manager: Refactoring for Clean Code
Introduction to Testing
Testing Forms and Validation
Using Database Transactions
Handling Global Exceptions
Preparing for Production
Environment Security Best Practices
Managing Assets in Production
Task Manager: Deployment Preparation