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('/'); });
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().
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With