6
0
Fork 0

添加初始化商品分类

release
vine_liutk 2021-12-29 10:00:41 +08:00
parent 239c82c929
commit cbc1e04014
2 changed files with 66 additions and 0 deletions

View File

@ -19,6 +19,7 @@ class DatabaseSeeder extends Seeder
AdminMenuSeeder::class,
AdminPermissionSeeder::class,
ProductPartSeeder::class,
ProductCategorySeeder::class,
AdAddressSeeder::class,
AppSettingSeeder::class,
]);

View File

@ -0,0 +1,65 @@
<?php
namespace Database\Seeders;
use App\Models\ProductCategory;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Throwable;
class ProductCategorySeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$categories = [
[
'name'=>'默认一级',
'is_show' => true,
'children'=>[
[
'name'=>'默认二级',
'is_show' => true,
'children'=>[
['name'=> '特膳视频', 'is_show' => true],
['name'=> '健康养生', 'is_show' => true],
['name'=> '美妆个护', 'is_show' => true],
['name'=> '生活用品', 'is_show' => true],
['name'=> '家纺用品', 'is_show' => true],
['name'=> '酒饮酒水', 'is_show' => true],
['name'=> '养生茗茶', 'is_show' => true],
['name'=> '洗护系列', 'is_show' => true],
],
],
],
],
];
DB::table('product_categories')->truncate();
try {
DB::begintransaction();
$this->createCategory($categories);
DB::commit();
} catch (Throwable $th) {
DB::rollBack();
report($th);
}
}
protected function createCategory($categories, $pid = null)
{
foreach ($categories as $category) {
$_category = ProductCategory::create([
'name' => $category['name'],
'is_show' => $category['is_show'],
'parent_id' => $pid,
]);
if (isset($category['children'])) {
$this->createCategory($category['children'], $_category->id);
}
}
}
}