How to Get Request Data in Laravel 5.2

This post explain how to get request data in routes.php file and in Laravel 5.2 controllers. The request data includes the parameters submitted via URL query string e.g. /search?category=furniture, here category is parameter name and furniture is parameter value. If a form is submitted via GET or POST, the data that is sent to server is also called request data. In short, request data is data that a request carries.


When Laravel framework receive an HTTP request, before transferring request processing to controllers, the framework extract data from request and instantiate an object of type Request, which is Laravel provided class. The Request object contain all the data that HTTP request carries. The Laravel framework passes the object using dependency injection into controller methods. It is passed only if we have expressed our interest by declaring an argument of type Request in controller methods. After receiving the Request object as argument, we can read the parameter values using different methods or attributes provided by the Request class.

We get data from Request object the same way, no matter the request data was submitted via query string by directly entering into URL or by submitting a GET or POST type HTML form. So, to keep it simple, I skip making HTML forms. Lets first map the /products/search URL to search method in ProductsController class.

Route::get('/products/search', 'ProductsController@search');


Get Parameter Value from Request Object by Parameter Name

Below is the code of ProductsController with search method that get request parameters by name.

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ProductsController extends Controller
{
    public function search(Request $request){
        $brand  = $request->input('brand');
        $category = $request->input('category');
        $model = $request->model;
        
        // Get products from database of received brand and category
        return "Brand = ". $brand . ", Category = " . $category . ", Model : " . $model;
    }
}

Here is the output user view in browser:


In search method above, I read parameter value by name using two different ways. First, by calling the input method of Request object passing it the parameter name. Second, we can access request parameters considering them public attributes of Request object. At runtime, the framework inject attributes into Request object after extracting from HTTP request parameters.

Get All or Selected Parameters from Request Object

Instead of getting parameter values by name (one by one), we may want to extract all parameters' values at once. We can get all parameters values using all method of Request object, which returns returns an associative arrays whose keys are request parameter names and values are request parameter values. See below code with output:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ProductsController extends Controller
{
    public function search(Request $request) {
        $allParams = $request->all();
        $content = "";

        foreach ($allParams as $key => $value) {
            $content .= $key . " = " . $value ."<br/>";
        }

        // $allparams is an associative array, you can also read individual element as $allparams['model']
        return $content;
    }



Sometimes, we are interested to read only specific parameters. There is another very convenient method named only that can be used to retrieve selected parameters. For example to read only brand and category, we can use following code:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ProductsController extends Controller
{
    public function search(Request $request){
        $selectedParams = $request->only(['brand', 'category']);
        $content = "";

        foreach ($selectedParams as $key => $value) {
            $content .= $key . " = " . $value . "<br/>";
        }

        return $content;
    }
}

Here is the output of above code:


You see, although the request contains other parameters i.e. model but only brand and category are displayed.

Comments

  1. Thanks for the post. Kanhasoft appreciates it. We are a top leading Laravel solution provider in India. We have 6+ years of experience and more than 100 happy clients. Visit our site if you have any question and requirement.

    ReplyDelete

Post a Comment