Chapter 7. Collecting and Handling User Data

Websites that benefit from a framework like Laravel often don’t just serve static content. Many deal with complex and mixed data sources, and one of the most common (and most complex) of these sources is user input in its myriad forms: URL paths, query parameters, POST data, and file uploads.

Laravel provides a collection of tools for gathering, validating, normalizing, and filtering user-provided data. We’ll look at those here.

Injecting a Request Object

The most common tool for accessing user data in Laravel is injecting an instance of the Illuminate\Http\Request object. It offers easy access to all of the ways users can provide input to your site: POSTed form data or JSON, GET requests (query parameters), and URL segments.

Other Options for Accessing Request Data

There’s also a request() global helper and a Request facade, both of which expose the same methods. Each of these options exposes the entire Illuminate Request object, but for now we’re only going to cover the methods that specifically relate to user data.

Since we’re planning on injecting a Request object, let’s take a quick look at how to get the $request object we’ll be calling all these methods on:

Route::post('form', function (Illuminate\Http\Request $request) {
    // $request->etc()
});

$request->all()

Just like the name suggests, $request->all() gives you an array containing all of the input the user has provided, from every source. Let’s say, for some reason, you ...

Get Laravel: Up & Running, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.