Add a easy way to find a collection by slug path #137
edcoreweb
started this conversation in
Feature Requests
Replies: 2 comments 1 reply
-
Currently, we fetch the |
Beta Was this translation helpful? Give feedback.
0 replies
-
Yes, the way we do it is similar, but was looking for a cleaner way than this: $pieces = explode('/', $slug);
$collections = Url::whereElementType(Collection::class)
->whereIn('slug', $pieces)
->with(['element'])
->orderByRaw('FIND_IN_SET(slug, ?)', [implode(',', $pieces)])
->get()
->map
->element;
// ensure we have found all pieces
abort_unless($collections->count() === count($pieces), 404);
// ensure path hierarchy
// a/b/c/d
// 1/20/5/30 => null/1/20/5
$idPath = $collections->map->id->join('/');
$parentPath = $collections->map->parent_id->push($collections->last()->id)->filter()->join('/');
abort_unless($idPath === $parentPath, 404); This only does 2 queries no mater how many nesting levels you have. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Would be nice to have a
findByPathOrFail
, method, where you could pass in the slug path, and get pack the collection.Would this be something you are willing to add in?
Beta Was this translation helpful? Give feedback.
All reactions