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.
The most common tool for accessing user data in Laravel is injecting an instance of the
Illuminate\Http\Request object. It provides easy access to all of the ways users can provide input to your site:
POST, posted JSON,
GET (query parameters), and URL segments.
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:
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 decided to have ...