Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined Method in Request::all()

I try the getting started guide from Laravel.com.

There is a chapter Creating the task. There is $request a parameter of the callback and in the function $request->all() is used to get the GET-Parameters. But if I execute that callback I get the error

Fatal error: Call to undefined method Illuminate\Support\Facades\Request::all()

Here is my code:

Route::post('/task', function(Request $request) {      $validator = Validator::make($request->all(), [         'name' => 'required|max:255',     ]);      if($validator->fails())         redirect('/')->withInput()->withErrors($validator);      $task = new Task();     $task->name = $request['name'];     $task->save();      return redirect('/'); }); 
like image 438
Gerrit Avatar asked Jan 08 '16 10:01

Gerrit


2 Answers

Your controller function gets injected an instance of Illuminate\Support\Facades\Request that forwards only static calls to underlying request object.

In order to fix that you need to import the underlying request class so that it is injected correctly. Add the following at the top of your routes.php file:

use Illuminate\Http\Request; 

or just call Request::all() instead of $request->all().

like image 133
jedrzej.kurylo Avatar answered Sep 29 '22 13:09

jedrzej.kurylo


Since this code is in the routes.php file, which is not namespaced, the Request object being injected into your closure is the Request facade, not the Illuminate\Http\Request object. The Request facade does not have an all() method.

Change your code to:

Route::post('/task', function(\Illuminate\Http\Request $request) {     // code }); 

Note: you generally don't fully qualify the Request object in Controller methods because Controllers usually add a use Illuminate\Http\Request; at the top. This is why your function definition in the routes file may look a little different than a controller method definition.

You can check out this answer for a little more information.

like image 30
patricus Avatar answered Sep 29 '22 13:09

patricus