Back to Blog
Lesson 21 of the Laravel Fundamentals: From Zero to Your First App course
LaravelJune 25, 20264 min read

Capturing User Input from Forms: A Laravel Beginner's Guide

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.

LaravelPHPWeb DevelopmentFormsRequest Handlingbackend

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.

Understanding the 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.

Building the Input Form

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.

HTML
style="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:

  1. The action attribute: This tells the browser where to send the data. We'll point this to our /tasks route.
  2. The method attribute: Always use POST for creating data.
  3. The 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.

Handling Input in the 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.

PHP
namespace 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.

Hands-on Exercise: Create the "Add Task" Workflow

It is time to advance our Task Manager project. Follow these steps:

  1. Define the Route: Add a POST route to routes/web.php that points to the store method of your TasksController.
  2. Create the Form: Ensure your create.blade.php has the correct action and method attributes.
  3. Handle the Data: In your TasksController@store, use the $request object to capture the input and return a simple string, like "Task received!", instead of dd().
  4. Verify: Navigate to your form in the browser, fill it out, and submit. You should see your confirmation message.

Common Pitfalls

Even experienced developers trip over these basics. Here is how to avoid them:

  • Missing 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.
  • Wrong HTTP Method: If your route is defined as 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.
  • Assuming the Request is Always Present: Beginners often try to access $request->input('title') before the user has submitted anything. Remember that this data only exists after the form is submitted to the controller action.

Recap

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.

Similar Posts