Reputation: 371
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
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
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
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
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