Why Laravel REST controller $request->input is NULL?
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
TomR
Updated on June 21, 2022Comments
-
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 almost 8 yearsIf you find it complicated to read a var_dump from the object size; do this:
echo '<pre>' . print_r($request) . '</pre>';
-
swatkins almost 8 yearsor in laravel:
dd($request);
-
Akshay Khale almost 8 yearsyou can try
dd($request->all())
instead...
-
-
TomR almost 8 yearsvar_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?