猿问

laravel 数据填充,如何优化这段代码?

在数据填充时遇到问题,之前都是采用随机数据填充,使用 laravel 模型工厂以及 Faker\Generator 生成随机数据。非常方便。但是我想在程序初始化时,填入一些由我提供的数据,而非随机数据。

假设:我提供的数据为 $name。我有两张表,categoriesnavigations ,两张表存在关联关系,navigations.category_id 对应 categories.id 。我的想法如代码所示,虽然能成功写入数据,但是感觉不好,感谢大佬们提供思路。

代码如下:

<?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class CategoryTableSeeder extends Seeder
{
    protected $name = [
        'php'      => [
            [
                'name'     => 'php1',
                'describe' => 'php1',
                'icon'     => 'php1'
            ], [
                'name'     => 'php1',
                'describe' => 'php1',
                'icon'     => 'php1'
            ], [
                'name'     => 'php1',
                'describe' => 'php1',
                'icon'     => 'php1'
            ],
        ], 'mysql' => [
            [
                'name'     => 'mysql1',
                'describe' => 'mysql1',
                'icon'     => 'mysql1'
            ], [
                'name'     => 'mysql1',
                'describe' => 'mysql1',
                'icon'     => 'mysql1'
            ], [
                'name'     => 'mysql1',
                'describe' => 'mysql1',
                'icon'     => 'mysql1'
            ],
        ], 'vue'   => [
            [
                'name'     => 'vue1',
                'describe' => 'vue1',
                'icon'     => 'vue1'
            ], [
                'name'     => 'vue1',
                'describe' => 'vue1',
                'icon'     => 'vue1'
            ], [
                'name'     => 'vue1',
                'describe' => 'vue1',
                'icon'     => 'vue1'
            ], [
                'name'     => 'vue1',
                'describe' => 'vue1',
                'icon'     => 'vue1'
            ], [
                'name'     => 'vue1',
                'describe' => 'vue1',
                'icon'     => 'vue1'
            ],
        ],
    ];

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run ()
    {
        foreach ($this->name as $key => $value) {
            $id = DB::table('categories')->insertGetId([
                'name'       => $key,
                'type'       => '1',
                'created_at' => \Carbon\Carbon::now()->toDateTimeString(),
                'updated_at' => \Carbon\Carbon::now()->toDateTimeString(),
            ]);
            foreach ($value as $k => $v) {
                DB::table('navigations')->insert([
                    'category_id' => $id,
                    'name'        => $v['name'],
                    'describe'    => $v['describe'],
                    'icon'        => $v['icon'],
                    'created_at'  => \Carbon\Carbon::now()->toDateTimeString(),
                    'updated_at'  => \Carbon\Carbon::now()->toDateTimeString(),
                ]);
            }
        }

    }
}
MM们
浏览 352回答 1
1回答

吃鸡游戏

假设你已经定义好了模型关联关系: collect($this->name)->map(function () {$item, $category} { $categoryModel = Category::create($category); // 模型关联填充 $categoryModel->navigations()->createMany($item); });
随时随地看视频慕课网APP
我要回答