Laravel get route parameter from request instance

Viewed 291

I have a route

Route::get('/category/{categorySlug}', 'CategoryController@showPage')->name('category.show');

Is it possible to get categorySlug from $request instance? I mean $request->get('categorySlug') Cause I dont see that param in there.

3 Answers

you can get the parameter using route in request:

 $request->route('parameter_name')

in this case:

 $request->route('categorySlug')

and you can get all parameters as array:

$request->route()->parameters

You can use categorySlug in method like as below :

public function showPage($categorySlug){
    // use $categorySlug here
}
  • Qutestion : Is it possible to get categorySlug from $request instance? I mean $request->get('categorySlug')

  • Answer : yes you can $request->get means you need to send data via get method which is like this

example.com?getKey=getdata here you can get getdata via $request->get('getKey')

Conclution

Route::get('/category', 'CategoryController@showPage')->name('category.show');

and send slug via example.com/category?categorySlug=slugdata then it will work

else if you want to work with old code like

Route::get('/category/{categorySlug}', 'CategoryController@showPage')->name('category.show');

then no need to pass any get param laravel can handel categorySlug by

public function showPage($categorySlug){ // you will get `categorySlug` as parameter
    // use $categorySlug here
}

or by $request->route('categorySlug') this also you can access

Related