public function index()
{
$competitions = Competition::orderBy('date', 'desc')->get();
return $this->show($competitions)
}
public function indexFinished()
{
$competitions = Competition::where('status', '<', '1')->orderBy('date', 'desc')->get();
return $this->show($competitions)
}
public function indexFuture()
{
$competitions = Competition::where('status', '>=', '1')->orderBy('date', 'desc')->get();
return $this->show($competitions);
}
protected function show($competitions)
{
return View::make('competitions.index', compact('competitions'));
}
You can do it with query strings as well, and you will have only one method and just a switch or a if/elseif statement, where you determine with competitions to show.
Another way, more cleaner, but, i don't know exactly how you're doing them, but i have an example below (didn't tested though)
public function index($show = null)
{
if ($status === 'finished')
{
$competitions = Competition::where('status', '<', '1')->orderBy('date', 'desc')->get();
}
elseif ($status === 'future')
{
$competitions = Competition::where('status', '>=', '1')->orderBy('date', 'desc')->get();
}
else
{
$competitions = Competition::orderBy('date', 'desc')->get();
}
return View::make('competitions.index', compact('competitions'));
}
You would basically have these routes
/competitions/ /competitions/finished/ /competitions/future/
Route::get('competitions/{status?}', 'CompetitionsController@index');
But as i said, didn't tested, so i might have missed something here and there =P
but using if else statment make it very hard to test? I think using if else just make it worse then my solution
If you are rendering the list is a better way to implement filters eg. /clients?status=active&orderBy=age,-name Parameters are passed in GET array. You have only one resource list method (in my example /clients)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community