I’m trying to do a multilevel find but it’s not going too well.
categories/parent
categories/parent/child
$result1 = page('categories')->index()->find('parent');
$result2 = page('categories')->index()->find('child');
Is it possible to do something like this? Kind of a flatten multilevel page find. I don’t know if the page is a parent or a child.
I’m in a route and right now I end up with 404 on children when I do this:
if( page('categories/' . $uid) ) return site()->visit('categories/' . $uid);
I also have a flat url structure:
http://example.com/parent
http://example.com/child
Any ideas?
You can use findBy()
:
$result1 = page('categories')->index()->findBy('uid', 'parent');
$result2 = page('categories')->index()->findBy('uid', 'child');
```
Note that this method only finds the first occurrence of a page with the given UID, which might not be the page you are looking for (if the same UID occurs several times). There's also a certain danger with using a flat structure because of that, as there is no way to make sure that the UID exists only once, so you have to be aware of this.
1 Like
Looks great!
I’ll try it after lunch. I already made a workaround that is much more ugly:
In a route:
$categories = page('categories')->index();
foreach($categories as $category) {
$category_obj[$category->slug()]= $category;
}
if( array_key_exists( $uid, $category_obj ) ) return site()->visit($category_obj[$uid]->uri());
I’m aware of the duplicate slug problem when using a flat structure.
Edit
It worked and the code is now much shorter (in a route):
$match = page('categories')->index()->findBy('uid', $uid);
if( $match ) return site()->visit( $match->uri() );
Thanks! 
1 Like