Jobs/RegenerateCategoryFullSlugs.php
private function getSlugParts(int $categoryId, string $locale): array
    {
        $category = $this->repo->getCategoriesPlain()->where('id', $categoryId)->first();
        $slugParts = [];

        if (!$category) {
            // Try to fetch from DB
            $category = Category::find($categoryId);

            if (!$category) {
                return [];
            }

            $category = [
                'id'        => $category->id,
                'parent_id' => $category->parent_id,
                'slugs'     => $category->translations->pluck('slug', 'locale')->toArray(),
            ];
        }

        $slugParts[] = array_get($category, 'slugs.' . $locale);

        if (isset($category['parent_id']) && $category['parent_id']) {
            $slugParts = array_merge($slugParts, $this->getSlugParts($category['parent_id'], $locale));
        }

        return $slugParts;
    }
private function getSlugParts(int $categoryId, string $locale): array
    {
        $category = $this->repo->getCategoriesPlain()->where('id', $categoryId)->first();
        $slugParts = [];

        if (!$category) {
            // Try to fetch from DB
            $category = Category::find($categoryId);

            if (!$category) {
                return [];
            }

            $category = [
                'id'        => $category->id,
                'parent_id' => $category->parent_id,
                'slugs'     => $category->translations->pluck('slug', 'locale')->toArray(),
            ];
        }

        $slugParts[] = array_get($category, 'slugs.' . $locale);

        if (isset($category['parent_id']) && $category['parent_id']) {
            $slugParts = array_merge($slugParts, $this->getSlugParts($category['parent_id'], $locale));
        }

        return $slugParts;
    }
private function getSlugParts ( int $ categoryId , string $ locale ) : array { $ category = $ this -> repo -> getCategoriesPlain ( ) -> where ( 'id' , $ categoryId ) -> first ( ) ; $ slugParts = [ ] ; if ( ! $ category ) { // Try to fetch from DB $ category = Category :: find ( $ categoryId ) ; if ( ! $ category ) { return [ ] ; } $ category = [ 'id' => $ category -> id , 'parent_id' => $ category -> parent_id , 'slugs' => $ category -> translations -> pluck ( 'slug' , 'locale' ) -> toArray ( ) , ] ; } $ slugParts [ ] = array_get ( $ category , 'slugs.' . $ locale ) ; if ( isset ( $ category [ 'parent_id' ] ) && $ category [ 'parent_id' ] ) { $ slugParts = array_merge ( $ slugParts , $ this -> getSlugParts ( $ category [ 'parent_id' ] , $ locale ) ) ; } return $ slugParts ; }
Get slug parts recursively without querying database

@param int $categoryId
@param string $locale
@return array
Get slug parts recursively without querying database