Čamo
Čamo

Reputation: 4160

Laravel get route parameter from request instance

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.

Upvotes: 3

Views: 569

Answers (3)

Kamlesh Paul
Kamlesh Paul

Reputation: 12391

  • 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

Upvotes: 0

OMR
OMR

Reputation: 12188

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

Upvotes: 3

Sohil Chamadia
Sohil Chamadia

Reputation: 426

You can use categorySlug in method like as below :

public function showPage($categorySlug){
    // use $categorySlug here
}

Upvotes: 0

Related Questions