Why Laravel REST controller $request->input is NULL?

13,292

Solution 1

You can access the input data with:

$input = $request->all();

https://laravel.com/docs/5.2/requests#retrieving-input

However, I've also had to get the input in this manner when using AngularJS $http module:

$input = file_get_contents('php://input');

Solution 2

for get all input

try it

$request = \Input::all();

Solution 3

If you want to fetch individual parameters from request object the you can do that with input Method of Request Class.

$request->input("parameter_name");

But if you want to fetch all request parameters then you can use all method which will return you an array of all the request key-value pairs

$request->all()

The thing you are missed is, you are calling $request->input which is null because input is method of Request class and not a property

Share:
13,292
TomR
Author by

TomR

Updated on June 21, 2022

Comments

  • TomR
    TomR almost 2 years

    I am following tutorial http://www.tutorials.kode-blog.com/laravel-5-angularjs-tutorial and I have managed to write the similar method for my controller:

    public function update(Request $request, $id) {
        $employee = Employee::find($id);
    
        $employee->name = $request->input('name');
        //...
        $employee->save();
    
        return "Sucess updating user #" . $employee->id;
    }
    

    It is thought in tutorial that this code works but in reality var_dump($request->input) gives NULL. So - what $request variable should I use for getting the body of the request? I made var_dump($request) but the structure is unmanageably large. Actually I am suscpicous about this tutorial - do we really need to list all the fields in the standard update procedure?

    • Jack
      Jack almost 8 years
      If you find it complicated to read a var_dump from the object size; do this: echo '<pre>' . print_r($request) . '</pre>';
    • swatkins
      swatkins almost 8 years
      or in laravel: dd($request);
    • Akshay Khale
      Akshay Khale almost 8 years
      you can try dd($request->all()) instead...
  • TomR
    TomR almost 8 years
    var_dump($request->all()) gives array(0) { }. This is strange because I am making test PUT request with body data from "I'm Only Resting" program. Where the body gets lost?