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

Sohail · · 211 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:

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

Install Laravel Valet Linux+ development environment on Ubuntu System

The official Laravel Valet development environment is great if you are an Apple user. But there is no official Valet for Linux or Window system.
Harish Kumar

Laravel Sanctum API Token Authentication Tutorial with example

Laravel Sanctum is a popular package for API Token Authentication. There are many other packages available to authenticate the APIs request in Laravel. For example, We are already (...)
Harish Kumar

Create SPA authentication Using Laravel Sanctum and Vue.js

In this guide, we will focus on SPA authentication in a simple Vue.js app using Laravel Sanctum. Laravel Sanctum provides a featherweight authentication system for SPAs (single (...)
Harish Kumar

Create API Authentication with Laravel Passport

In this article, we'll see how to implement restful API authentication using Laravel Passport. You should have experience working with Laravel as this is not an introductory tutorial. (...)
Sumit Talwar

Laravel Themer: multi-theme support for Laravel application

This Laravel Themer package adds multi-theme support to your application. This theme package improves any application while allowing the freedom to organize and maintain your app's (...)
Harish Kumar