Ad

Undefined Method In Request::all()

- 1 answer

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('/');
});
Ad

Answer

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().

Ad
source: stackoverflow.com
Ad