Support the ongoing development of Laravel.io →
Database Architecture
Last updated 1 year ago.
0

If keeping your urls in a valid hierarchy is important, then you can probably do a check like so:

Route::get('category/{hierarchy}', function($hierarchy)
{
	$categories = explode('/', $hierarchy);

	$main = Category::where('slug', end($categories))->first();
	reset($categories);

	if ($main)
	{
		$ancestors = $main->getAncestors();

		$valid = true;

		foreach ($ancestors as $i => $category)
		{
			if ($category->slug !== $categories[$i])
			{
				$valid = false;
				break;
			}
		}

		if ($valid)
		{
			/* continue on with your code here... */
		}
	}

	App::abort('404');

})->where('hierarchy', '(.*)?');

If keeping the parent categories isn't important,then you can get rid of most of the above code, and instead just grab the end of $categories and use that end slug as you would normally.

Then you have to implode the ancestors on your URLs using '/' as the glue. Like so:

<a href="/category/'.implode('/', $category->getAncestorsAndSelf()->lists('slug')).'">link</a>
Last updated 1 year ago.
0

You could always split the slug field into two:

slugs: parent1-slug/parent2-slug/child-slug slug: child-slug

Last updated 1 year ago.
0

@andrewsuzuki

That should work, i'll test it later and let you know. Thanks for your response.

Last updated 1 year ago.
0

There is no point doing that, you can just do this...

Route::get('test/{one}/{two}', function($one, $two)
{
	return 'two: ' . $one . ', ' . $two;
});

Route::get('test/{one}', function($one)
{
	return 'one: ' . $one;
});
Last updated 1 year ago.
0

I'm sorry @ConnorVG, but @andrewsuzuki-s version was exactly what I was looking for.

Last updated 1 year ago.
0

Yea, ConnerVG's solution means manually determining the full depth. The initial solution should "just work" no matter the number of children (which in some cases can be quite heavy.)

Last updated 1 year ago.
0

Oh, I see... I didn't noticed you wished for more than just 1 child. Sorry bud!

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Landish landish Joined 10 Mar 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.