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

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

Laravel Facades: Simplifying Code and Improve Readability

As an integral part of Laravel, a renowned PHP framework, Facades provide a static interface to classes stored in the application's service container. They serve as static proxies (...)
Harish Kumar

What is Laravel’s Service Container and How to Use Dependency Injection in Laravel App

Dependency injection and inversion of control are vital in clean web development. They make writing maintainable, testable code possible. Laravel is a famous PHP framework that (...)
Harish Kumar

Secure Your SPA with Laravel Sanctum: A Step-by-Step Guide

In today's web development landscape, Single Page Applications (SPAs) are increasingly popular. But securing their interaction with backend APIs is crucial. Laravel Sanctum provides (...)
Harish Kumar

Multi-Authentication with Guards in Laravel

Laravel's robust authentication system provides a powerful mechanism for securing your application. To cater to scenarios where you need different user roles with distinct login (...)
Harish Kumar

Laravel Pint & VS Code: Automate Your Code Formatting

Laravel Pint is an opinionated PHP code style fixer built on top of PHP-CS-Fixer, designed to simplify the process of ensuring clean and consistent code style in Laravel projects. (...)
Harish Kumar

Laravel Clockwork: A Deep Dive into Debugging, Profiling Skills and Best Practices

In the world of web development, building complex applications often comes with the challenge of identifying and resolving performance bottlenecks. This is where a reliable debugging (...)
Harish Kumar