Difference between Laravel $request->input(), $request->get(), and $request->name direct property

Sohail · · 9406 Views

If you’ve been around Laravel for a while, you might have seen there are three ways in controllers to retrieve inputs from the submitted form. For example, if you are trying to retrieve the name of a user from a form. The user controller might have one of the following lines of code:

public function store(Request $request)
{
     $name = $request->input(‘name’);
     // Or
     $name = $request->name;
     // Or
     $name = $request->get(‘name’);
}

Now the question is what the difference is between these three techniques, and which one is preferred?

$request->input()

From the Laravel 5.3 docs, there are three distinctions that set the $request->input() method apart.

  1. The  $request->input() method can be used with any HTTP verb to retrieve data (both GET or POST request).

$name = $request->input('name');
  1. A default value can be set when using the $request->input() method by adding a 2nd (optional) parameter.

$name = $request->input('name', 'Sally');
  1. You can use dot notation to access forms that have names that are arrays. For example, if you were using a form with a name like this:

<input type="text" name="products[0][name]">

then your controller can retrieve that form input using:

$name = $request->input('products.0.name');

Direct property

When using direct properties, Laravel will first look for the parameter’s value in the request payload. If it is not present, Laravel will search for the field in the route parameters. The dynamic property will return the input whether the input is coming from a GET or POST request.

$name = $request->name;

$request->get()

The $request->get() method can be confused with the Eloquent and Query Builder get() methods that retrieve results from a query. The below code block is a bit blurry in terms of what get() method is retrieving data from the database and which one is retrieving data from the form:

$qb_users = DB::table('users')->get();
 
$eloquent_user = App\User::find(1)->get();
 
$input_user = $request->get(‘user’);

You might think that it’s not so bad having a couple of different get() methods, even if they are all frequently used. This means the end result could end up looking something like this:

Route::get($uri2,$callback);
 
Route::get($uri3,$callback);
 
Route::get('user/{id}', function ($id) {

$qb_users = DB::table('users')->get()
 
$qb_names = DB::table('names')->get()
 
$qb_numbers = DB::table('numbers')->get()
 
$qb_comments = DB::table('comments')->get();
 
$eloquent_user1 = App\User::find(1)->get();
 
$eloquent_user2 = App\User::find(2)->get();
 
$eloquent_user3 = App\User::find(3)->get();
 
$eloquent_user4 = App\User::find(4)->get();
 
$input_user1 =$request->get(‘user1’);
 
$input_user2 =$request->get(‘user2’);
 
$input_user3 =$request->get(‘user3’);
 
$input_user4 =$request->get(‘user4’);
0

Please login or create new account to add your comment.

0 comments
You may also like:

Part #3: Rule objects based custom validation in Laravel

Laravel comes with multiple ways to add custom validation rules to validate form request inputs. I have already explained some of the ways in the following article links:
Harish Kumar

Part #2: How to use Laravel's Validator::extend method for custom validation

Validation is important in any application as it validates a form before performing actions on it. It allows the user to know their input is accurate and confident about the operation (...)
Harish Kumar

Part #1: Closure-based Custom Laravel Validation

While I was working with Laravel, validation using closure came to my mind, and I know it will be helpful to you. This tutorial assists you with all what is the difference between (...)
Harish Kumar

How to use the enumerations(Enums) of PHP 8.1 in Laravel?

The release of PHP 8.1 brings native enumerations to PHP. There is no more requirement for custom solutions in your Laravel projects since the Laravel v8.69 release has you back. (...)
Harish Kumar

Mobile App Development Process

With businesses adopting a mobile-first approach and the growing number of mobile apps, successful mobile app development seems like a quest. But it’s the process that determines (...)
Narola Infotech

What are Laravel Macros and How to Extending Laravel’s Core Classes using Macros with example?

Laravel Macros are a great way of expanding Laravel's core macroable classes and add additional functionality needed for your application. In simple word, Laravel Macro is an (...)
Harish Kumar