Skip to content

Instantly share code, notes, and snippets.

@adumskis
Created February 13, 2017 06:21
Show Gist options
  • Save adumskis/602718e6d5a46b16ec1d714d56f82b4f to your computer and use it in GitHub Desktop.
Save adumskis/602718e6d5a46b16ec1d714d56f82b4f to your computer and use it in GitHub Desktop.
Unlimited parameters in route
<?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'));
}
<?php
//Should be last in routes list
Route::get('{slugs_string}', [
'as' => 'front.page.show', 'uses' => 'PageController@show'
])->where('slugs_string', '.*');
@d3v2a
Copy link

d3v2a commented Feb 14, 2017

A query in a loop ? Maybe use whereOr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment