TomR
TomR

Reputation: 3056

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

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?

Upvotes: 5

Views: 16693

Answers (3)

jignesh patel
jignesh patel

Reputation: 131

for get all input

try it

$request = \Input::all();

Upvotes: 2

Akshay Khale
Akshay Khale

Reputation: 8371

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

Upvotes: 1

swatkins
swatkins

Reputation: 13640

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');

Upvotes: 4

Related Questions