Created
February 13, 2017 06:21
-
-
Save adumskis/602718e6d5a46b16ec1d714d56f82b4f to your computer and use it in GitHub Desktop.
Unlimited parameters in route
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function show($slugs_string){ | |
$slugs = explode('/', $slugs_string); | |
$page = null; | |
foreach($slugs as $key => $slug){ | |
$page = Page::where('slug', $slug) | |
->where('parent_id', '=', is_null($page)?null:$page->id) | |
->firstOrFail(); | |
// Another solution: | |
// ->first() | |
// If $page === null return 404 error | |
if($key == 0 && $page->parent){ | |
return redirect()->route('front.main.home'); | |
} | |
} | |
return view('front.page.show', compact('page')); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Should be last in routes list | |
Route::get('{slugs_string}', [ | |
'as' => 'front.page.show', 'uses' => 'PageController@show' | |
])->where('slugs_string', '.*'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A query in a loop ? Maybe use whereOr