在 Laravel 6 中,从一系列的文件列表中,取出不重复的特定目录列表
1、现在表中的记录。如图1
2、查询出文件列表的实现如下
1 | $themeAssets = ThemeAsset::select( 'asset_key' )->where( 'theme_id' , '9994a913-4383-423c-993e-21acde47dbe3' )->where( 'category' , ThemeAsset::CATEGORY_MIGRATION)->get(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | 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、由于需要取出文件:migrations/v2.0.28/migrate_settings_data.php 中的 v2.0.28 部分。其中有一些文件:migrations/migrate_cart.blade.php 不存在 v2.0.28 部分,所以需要先过滤掉。先后使用了集合的 filter()、mapToGroups() 方法。
1 2 3 4 5 6 7 | $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、打印结果如下,符合预期。如图2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | 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、仅获取集合中的数组的键。如图3
1 2 | print_r( array_keys ( $grouped ->all())); exit ; |
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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 ) |
近期评论