In Laravel 8, $request->all()
is not showing dynamic path parameters.
Steps to reproduce:
- Start a new Laravel project with composer, Laravel version 8.61.0.
- Define a route in
routes/api.php
as follows:
Route::get('/first/{first}/second/{second}', function (Request $request) {
return $request->all();
});
Run
php artisan serve
and direct the browser tohttp://localhost:8000/api/first/hello/second/world
Expect something like the following response:
{"first":"hello","second":"world"}
. Instead we simply see[]
.Change the route to:
Route::get('/first/{first}/second/{second}', function (Request $request) {
return [
'first'=>$request->first,
'second'=>$request->second,
];
});
- Then we see the expected
{"first":"hello","second":"world"}
So...why isn't $request->all()
giving me this response?
CodePudding user response:
I looked at Laravel doc
You may retrieve all of the incoming request's input data as an
array
using theall
method. This method may be used regardless of whether the incoming request is from an HTML form or is an XHR request
Means $request->all()
equal to $request->input('name', 'email', ....)
instead of $request->all()
you can try this
Route::get('/first/{first}/second/{second}', function (Illuminate\Http\Request $request, $first, $second) {
return [
'first'=> $first,
'second'=> $second,
];
});
UPDATE
You can do it too with (splat operator in PHP)
Route::get('/first/{first}/second/{second}', function (Illuminate\Http\Request $request, ...$all) {
return $all;
});
// returns
[
"hello",
"world"
]
I hope this helped you,
Happy Coding.
CodePudding user response:
Have you tried dd($request->all())
statement in Controller, not in closure. In previous Laravel version (version 5 or version 7), that state works in that versions.