Support the ongoing development of Laravel.io →
posted 10 years ago
Architecture
Last updated 2 years ago.
0
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.

Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

but using if else statment make it very hard to test? I think using if else just make it worse then my solution

Last updated 2 years ago.
0

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)

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

dcrystalj dcrystalj Joined 2 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.