Search

Laravel 4: Getting User Input

November 2, 2013 —John Koster

No matter what application you're developing, or what platform you are developing on, it is a safe bet you need to get input from your users some how. Laravel 4 provides a really convenient way to do this using the Input facade. Let's look at a simple form in Laravel:

1{{ Form::open(array('url' => 'user/create', 'method' => 'post')) }}
2 <input type="text" name="firstName">
3 <input type="text" name="lastName">
4 <input type="submit" value="Create me!">
5{{ Form::close() }}

Our form has two fields: a firstName and a lastName. They will be used to enter our user's first and last name (crazy). For the sake of simplicity, we will use a route to handle the action of our form instead of a controller (you really should be using controllers for stuff like this):

1<?php
2 
3Route::post('user/create', function()
4{
5 // Handle the user create form.
6});

#Getting an Input Element's Value

Inside of our route, we can use the get method on the Input facade to get our input, and it really is as easy as doing this:

1<?php
2 
3Route::post('user/create', function()
4{
5 // Get the user's first and last name.
6 $firstName = Input::get('firstName');
7 $lastName = Input::get('lastName');
8});

#Specifying a Default Value

When we use the get() method, we pass the name of the field we want the value of as the first argument. We can optionally use a second argument to specify a default value if one has not been set:

1<?php
2 
3Route::post('user/create', function()
4{
5 // Get the user's first and last name.
6 $firstName = Input::get('firstName', 'John');
7 $lastName = Input::get('lastName', 'Doe');
8});

What the above code will do is first look to see if the user has entered their first and last name. If they did not enter a first name our $firstName variable will contain the value John. Likewise if they didn't supply a last name the $lastName variable would be set to Doe.

We can also get all of the input values by using the all() method. This will return an associative array where the keys are the field name and their associated user input.

1<?php
2 
3Route::post('user/create', function()
4{
5 // Get the user input as an array.
6 $input = Input::all();
7 
8 // Just dump the array to the page.
9 var_dump($input);
10});

When this code runs, we will see something similar to this on the page:

1array (size=3)
2 '_token' => string 'vhT6UIOCVALts10RiSMtqYNO1UyjyHf7OfV8uGlH' (length=40)
3 'firstName' => string 'John' (length=4)
4 'lastName' => string 'Doe' (length=3)

#I'm Getting The Wrong Data?

So we know how to get the input from the user, but how do we handle the occasional situation where we have ambiguous input?

For example say we have a form field named name set to John Doe for a user's name using a POST request. In addition we also have a URL query parameter with the same name set to accounting for some other purpose. If we use the following code what value would we get?

1<?php
2 
3Route::post('user/create', function()
4{
5 $name = Input::get('name');
6});

We would get the user's name, and not the accounting value. This is because when we use the get() function, data from a POST request have higher precedence over query parameters. To get around this issue we can specify the source of the data we want.

First we are going to have to work with an actual instance of the input object instead of using the Input facade. We have to do this in order to access the instance's properties.

1<?php
2 
3Route::post('user/create', function()
4{
5 
6 $requestData = Input::instance();
7 
8});

The instance() method returns the current request instance. When we store a reference to this in a variable (instead of using the Input facade) we can access its public properties. Each request object has a query and a request public property.

Tip: The Input facade and the Request facade provide access to the same object instance. This means that you can also use Request::get() for your input. However, you should use whichever facade makes sense for your current task for optimum readability.

This next part should look familiar to user's coming from Symphony 2. We can access the following public properties and use their get() method:

1<?php
2 
3Route::post('user/create', function()
4{
5 
6 $requestData = Input::instance();
7 
8 $userName = $requestData->request->get('name');
9 
10 $queryName = $requestData->query->get('name');
11 
12 var_dump($userName, $queryName);
13 
14});

When the above code runs we should see something like this in the browser:

1string 'asdf' (length=4)
2string 'accountant' (length=10)

The request property contains our POST data and the query property contains our URL query data (also known as GET data). Both properties have a get() method that works the same way as Input::get() except that we are explicitly saying we want POST data or that we specifically want GET data.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.