没有不值得去解决的问题,也没有不值得去学习的技术!

Extract Unique Directories From a File List in Laravel 6

Current records in the table

1. The current records in the table are shown in Figure 1.

Current records in the table
Figure 1

2. The implementation for querying the file list is as follows:

PHP

$themeAssets = ThemeAsset::select('asset_key')->where('theme_id', '9994a913-4383-423c-993e-21acde47dbe3')->where('category', ThemeAsset::CATEGORY_MIGRATION)->get();

Plaintext

Illuminate\Database\Eloquent\Collection Object
(
    [items:protected] => Array
        (
            [0] => Modules\ThemeStoreDb\Models\ThemeAsset Object
                (
                    [forceDeleteVersion:protected] => 
                    [table:protected] => theme_asset2
                    [connection:protected] => mysql
                    [primaryKey:protected] => id
                    [keyType:protected] => int
                    [incrementing] => 1
                    [with:protected] => Array
                        (
                        )

                    [withCount:protected] => Array
                        (
                        )

                    [perPage:protected] => 15
                    [exists] => 1
                    [wasRecentlyCreated] => 
                    [attributes:protected] => Array
                        (
                            [asset_key] => migrations/migrate_cart.blade.php
                        )

                    [original:protected] => Array
                        (
                            [asset_key] => migrations/migrate_cart.blade.php
                        )

                    [changes:protected] => Array
                        (
                        )

                    [casts:protected] => Array
                        (
                        )

                    [dates:protected] => Array
                        (
                        )

                    [dateFormat:protected] => 
                    [appends:protected] => Array
                        (
                            [0] => created_at
                            [1] => updated_at
                        )

                    [dispatchesEvents:protected] => Array
                        (
                        )

                    [observables:protected] => Array
                        (
                        )

                    [relations:protected] => Array
                        (
                        )

                    [touches:protected] => Array
                        (
                        )

                    [timestamps] => 1
                    [hidden:protected] => Array
                        (
                        )

                    [visible:protected] => Array
                        (
                        )

                    [guarded:protected] => Array
                        (
                            [0] => *
                        )

                )

            [1] => Modules\ThemeStoreDb\Models\ThemeAsset Object
                (
                    [forceDeleteVersion:protected] => 
                    [table:protected] => theme_asset2
                    [connection:protected] => mysql
                    [primaryKey:protected] => id
                    [keyType:protected] => int
                    [incrementing] => 1
                    [with:protected] => Array
                        (
                        )

                    [withCount:protected] => Array
                        (
                        )

                    [perPage:protected] => 15
                    [exists] => 1
                    [wasRecentlyCreated] => 
                    [attributes:protected] => Array
                        (
                            [asset_key] => migrations/migrate_collections.blade.php
                        )

                    [original:protected] => Array
                        (
                            [asset_key] => migrations/migrate_collections.blade.php
                        )

                    [changes:protected] => Array
                        (
                        )

                    [casts:protected] => Array
                        (
                        )

                    [dates:protected] => Array
                        (
                        )

                    [dateFormat:protected] => 
                    [appends:protected] => Array
                        (
                            [0] => created_at
                            [1] => updated_at
                        )

                    [dispatchesEvents:protected] => Array
                        (
                        )

                    [observables:protected] => Array
                        (
                        )

                    [relations:protected] => Array
                        (
                        )

                    [touches:protected] => Array
                        (
                        )

                    [timestamps] => 1
                    [hidden:protected] => Array
                        (
                        )

                    [visible:protected] => Array
                        (
                        )

                    [guarded:protected] => Array
                        (
                            [0] => *
                        )

                )
        )

)


3. I needed to extract the v2.0.28 portion from the file: migrations/v2.0.28/migrate_settings_data.php. Some files, such as migrations/migrate_cart.blade.php, do not contain the v2.0.28 segment, so they must be filtered out first. I used the collection’s filter() and mapToGroups() methods sequentially.

PHP

        $grouped = $themeAssets->filter(function ($value) {
            return Str::startsWith(explode('/', $value->asset_key)[1], 'v2');
        })->mapToGroups(function ($value) {
            return [explode('/', $value->asset_key)[1] => $value->asset_key];
        });
        print_r($grouped);
        exit;

4. The printed results are as follows, matching expectations. See Figure 2.

The printed results are as follows, matching expectations
Figure 2
Plaintext

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [v2.0.20] => Illuminate\Database\Eloquent\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => migrations/v2.0.20/migrate_settings_data.php
                        )

                )

            [v2.0.21] => Illuminate\Database\Eloquent\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => migrations/v2.0.21/migrate_settings_data.php
                        )

                )

            [v2.0.28] => Illuminate\Database\Eloquent\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => migrations/v2.0.28/migrate_cart.php
                            [1] => migrations/v2.0.28/migrate_collections.php
                            [2] => migrations/v2.0.28/migrate_collectiontitem.php
                            [3] => migrations/v2.0.28/migrate_index.php
                            [4] => migrations/v2.0.28/migrate_product_detail.php
                            [5] => migrations/v2.0.28/migrate_search.php
                            [6] => migrations/v2.0.28/migrate_settings_data.php
                        )

                )

            [v2.1.1] => Illuminate\Database\Eloquent\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => migrations/v2.1.1/migrate_index.php
                        )

                )

            [v2.1.2] => Illuminate\Database\Eloquent\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => migrations/v2.1.2/migrate_settings_data.php
                        )

                )

            [v2.1.3] => Illuminate\Database\Eloquent\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => migrations/v2.1.3/migrate_cart.php
                            [1] => migrations/v2.1.3/migrate_collections.php
                            [2] => migrations/v2.1.3/migrate_collectiontitem.php
                            [3] => migrations/v2.1.3/migrate_index.php
                            [4] => migrations/v2.1.3/migrate_product_detail.php
                            [5] => migrations/v2.1.3/migrate_search.php
                        )

                )

            [v2.1.30] => Illuminate\Database\Eloquent\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => migrations/v2.1.30/migrate_settings_data.php
                        )

                )

            [v2.1.65] => Illuminate\Database\Eloquent\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => migrations/v2.1.65/migrate_product_detail.php
                            [1] => migrations/v2.1.65/migrate_settings_data.php
                        )

                )

            [v2.1.70] => Illuminate\Database\Eloquent\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => migrations/v2.1.70/migrate_settings_data.php
                        )

                )

            [v2.1.80] => Illuminate\Database\Eloquent\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => migrations/v2.1.80/migrate_product_detail.php
                        )

                )

        )

)


5. Retrieve only the keys of the array in the collection. See Figure 3.

Retrieve only the keys of the array in the collection
Figure 3
PHP

        print_r(array_keys($grouped->all()));
        exit;

Plaintext

Array
(
    [0] => v2.0.20
    [1] => v2.0.21
    [2] => v2.0.28
    [3] => v2.1.1
    [4] => v2.1.2
    [5] => v2.1.3
    [6] => v2.1.30
    [7] => v2.1.65
    [8] => v2.1.70
    [9] => v2.1.80
)


关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系