How Can I Retrieve Input Data In Laravel with Example

admin_img Posted By Bajarangi soft , Posted On 17-09-2020

The input values can be easily retrieved in Laravel. No matter what method was used “get” or “post”, the Laravel method will retrieve input values for both the methods the same way. There are two ways we can retrieve the input values. Using the input() method. Using the properties of Request instance.

How Can I Retrieve Input Data In Laravel with Example

Retrieving All Input Data

You may also retrieve all of the input data as an array using the all method:

$input = $request->all();
 

Retrieving An Input Value

Using a few simple methods, you may access all of the user input from your Illuminate\Http\Request instance without worrying about which HTTP verb was used for the request. Regardless of the HTTP verb, the input method may be used to retrieve user input:

$name = $request->input('name');


You may pass a default value as the second argument to the input method. This value will be returned if the requested input value is not present on the request:
$name = $request->input('name', 'Sally');


When working with forms that contain array inputs, use "dot" notation to access the arrays:

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

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


You may call the input method without any arguments in order to retrieve all of the input values as an associative array:

$input = $request->input();
 

Retrieving Input From The Query String

While the input method retrieves values from entire request payload (including the query string), the query method will only retrieve values from the query string:

$name = $request->query('name');


If the requested query string value data is not present, the second argument to this method will be returned:

$name = $request->query('name', 'Helen');
 

You may call the query method without any arguments in order to retrieve all of the query string values as an associative array:
 

$query = $request->query();


Retrieving Input Via Dynamic Properties

You may also access user input using dynamic properties on the Illuminate\Http\Request instance. For example, if one of your application's forms contains a name field, you may access the value of the field like so:
 

$name = $request->name;


When using dynamic 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.

Retrieving JSON Input Values

When sending JSON requests to your application, you may access the JSON data via the input method as long as the Content-Type header of the request is properly set to application/json. You may even use "dot" syntax to dig into JSON arrays:

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


Retrieving Boolean Input Values

When dealing with HTML elements like checkboxes, your application may receive "truthy" values that are actually strings. For example, "true" or "on". For convenience, you may use the boolean method to retrieve these values as booleans. The boolean method returns true for 1, "1", true, "true", "on", and "yes". All other values will return false:

$archived = $request->boolean('archived');


Retrieving A Portion Of The Input Data

If you need to retrieve a subset of the input data, you may use the only and except methods. Both of these methods accept a single array or a dynamic list of arguments:

$input = $request->only(['username', 'password']);

$input = $request->only('username', 'password');

$input = $request->except(['credit_card']);

$input = $request->except('credit_card');


Determining If An Input Value Is Present

You should use the has method to determine if a value is present on the request. The has method returns true if the value is present on the request:

if ($request->has('name')) {
//
}


When given an array, the has method will determine if all of the specified values are present:

if ($request->has(['name', 'email'])) {
//
}


The hasAny method returns true if any of the specified values are present:

if ($request->hasAny(['name', 'email'])) {
//
}


If you would like to determine if a value is present on the request and is not empty, you may use the filled method:

if ($request->filled('name')) {
//
}


To determine if a given key is absent from the request, you may use the missing method:

if ($request->missing('name')) {
//
}


For Example

1 − Create a Registration form, where user can register himself and store the form at resources/views/register.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Retrieving Input In laravel</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
   
    <div class="text-center">
        <h1>Retrieving Input In laravel</h1>
    </div>
  
    <div class="form-group col-sm-2"></div>
    <div class="form-group col-sm-8">
        <div class="well">
            <form method="post" action="{{url('/user/register')}}">
                @csrf
                <div class="form-group col-sm-12">
                    <label>UserName</label>
                    <input  class="form-control" type="text" name="username" value="">
                </div>
                <div class="form-group col-sm-12">
                    <label>Password</label>
                    <input  class="form-control" type="text" name="password" value="">
                </div>
                <div class="form-group text-center">
                    <input  class="btn btn-primary" type="submit" >
                </div>
            </form>
            <div class="form-group col-sm-2"></div>
        </div>
    </div>
    <br>
</div>
</body>
</html>


2 − Execute the below command to create a UserRegistration controller.

php artisan make:controller UserRegistration --plain

 

3− Copy the following code in app/Http/Controllers/UserRegistration.php controller.

app/Http/Controllers/UserRegistration.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserRegistration extends Controller {
    public function storeRegister(Request $request) {
     
        //Retrieve the username input field
        $username = $request->username;
        echo 'Username: '.$username;
        echo '<br>';

        //Retrieve the password input field
        $password = $request->password;
        echo 'Password: '.$password;
    }
}

 

4 − Add the following line in app/Http/routes.php file.

app/Http/routes.php

Route::get('/register',function() {
return view('register');
});
Route::post('/user/register',array('uses'=>'UserRegistration@storeRegister'));


5 − Visit the following URL and you will see the registration form as shown in the below figure. Type the registration details and click Register and you will see on the second page that we have retrieved and displayed the user registration details.
 

​http://localhost/laraveldemoproject/public/register

Related Post