66 lines
2.0 KiB
PHP
66 lines
2.0 KiB
PHP
<?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);
|
|
}
|
|
}
|
|
}
|
|
}
|