Ad

Can We Connect Laravel Routing Parameter And Controller Parameter?

- 1 answer

So I want to fill key but it fill another parameter when inside controller and i see it will fill the first parameter first. Is There Anyway to make key? will go to $key parameter and type? to $type and fill? to $fill too ?

I'm Using Laravel 5.6.*

Route::get('/report', '[email protected]')->name('api.report');
Route::get('/report/all', '[email protected]')->name('api.report.all');
Route::get('/report/all/key/{key?}', '[email protected]')->name('api.report.all.key');
Route::get('/report/all/search/{type?}/{fill?}', '[email protected]')->name('api.report.all.type.fill');
Route::get('/report/all/search/{type?}/{fill?}/key/{key?}', '[email protected]')->name('api.report.all.type.fill.key');

expected result : null null testing /report/all/key/testing

public function all($type = null,$fill = null,$key = null)
{
    dd($type.$fill.$key); 
}

actual result : testing null null /report/all/key/testing

public function all($type = null,$fill = null,$key = null)
{
    dd($type.$fill.$key); 
}
Ad

Answer

You could replace the parameters inside all() and type-hint Illuminate\Http\Request instead.

Then, you could just do this:

use Illuminate\Http\Request; 

public function all(Request $request)
{
    $key  = $request->key;
    $type = $request->type;
    $fill = $request->fill;

    dd($type.$fill.$key); 
}
Ad
source: stackoverflow.com
Ad