stepbystep
stepbystep

Reputation: 371

How to pass a parameter in routes to the controller?

I am trying to refactor my code. If I could pass an argument in the routes page to the controller where the function is, then I could refactor many of function that are almost identical.

Something like this in Router:

Route::get('/entrepreneurs', 'HomeController@show')->withParameter('enterpreneur');

Which gives me something like this in Controller:

public function entrepreneurs($withParameter){
   $entrepreneurs = DB::table('stars')->where('type', '=', $withParameter)->get();

   return view('entrepreneurs', [
     'entrepreneurs' => $entrepreneurs,
   ]);
 }

Is this possible?

-------- Update --------

Some of you are suggestion that I use Route Parameters:

Route::get('/entrepreneurs/{paramName}', 'HomeController@show');

However, I already use Route Model Binding to access individual pages (e.g. www.mywebsite.com/entrepreneurs/Mark_Zuckerberg)

So this is a conflicting with the solutions you guys provided!

Upvotes: 1

Views: 247

Answers (4)

Benjamin Brasseur
Benjamin Brasseur

Reputation: 558

If you want to pass param in your routes

Route::get('/entrepreneurs/type/{paramName}', 'HomeController@show');

And for an optionnal param :

Route::get('/entrepreneurs/type/{paramName?}', 'HomeController@show');

With optionnal paramater it should look like this in your controller :

public function show($paramName = null){
   $entrepreneurs = DB::table('stars')->where('type', '=', $paramName)->get();

   return view('entrepreneurs', [
     'entrepreneurs' => $entrepreneurs,
   ]);
 }

You can have more information here : https://laravel.com/docs/5.4/routing#route-parameters

Upvotes: 0

Jean Marcos
Jean Marcos

Reputation: 1177

You can also do:

// --------------- routes ---------------------

Route::get("page", [
    "uses" => 'HomeController@show',
    "entrepreneurs" => "value"
]);

// -------------- controller -------------------

public function show(Request $request)
{
    $entrepreneurs = DB::table('stars')->where('type', '=', $request->route()->getAction()["entrepreneurs"])->get();

    return view('entrepreneurs', [
        'entrepreneurs' => $entrepreneurs,
    ]);
}

Upvotes: 1

linktoahref
linktoahref

Reputation: 7972

Routes:

Route::get('/entrepreneurs/{enterpreneur}', 'HomeController@show');

HomeController.php:

public function show($enterpreneur = "") 
{
   $entrepreneurs = DB::table('stars')->where('type', '=', $enterpreneur)->get();

   return view('entrepreneurs', [
     'entrepreneurs' => $entrepreneurs,
   ]);

}

To pass a static variable along with route

Route::get('/entrepreneurs', 'HomeController@show')->defaults('enterpreneur', 'value');

and access them in your controller as

public function show(Request $request) 
{
    $entrepreneur = $request->route('entrepreneur');
    $entrepreneurs = DB::table('stars')->where('type', '=', $enterpreneur)->get();

    return view('entrepreneurs', [
      'entrepreneurs' => $entrepreneurs,
    ]);

}

https://laravel.com/docs/5.4/routing#route-parameters

Upvotes: 1

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

I think you can try this:

Route::get('/entrepreneurs/{enterpreneur}', 'HomeController@show');

public function entrepreneurs($enterpreneur){
  $entrepreneurs = DB::table('stars')->where('type', '=', $enterpreneur)->get();

  return view('entrepreneurs', [
   'entrepreneurs' => $entrepreneurs,
  ]);
}

Hope this help for you !!!

Upvotes: 0

Related Questions