修改 backpack SettingsTableSeeder 逻辑,只插入新增的配置

文章目录

    Laravel backpack SettingsTableSeeder 的默认逻辑非常不人性化,直接 truncate 配置表,然后重新写入配置。

    php artisan db:seed --class=SettingsTableSeeder
    

    对于线上系统已有配置的情况下,非常不合理。

    所以将逻辑调整了一下,改成了只写入不存在的 key。

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // truncate table
        // DB::table('settings')->truncate();
    
        foreach ($this->settings as $index => $setting) {
            $record = DB::table('settings')->where('key', $setting['key'])
                ->first();
    
            if ($record) {
                $this->command->info($setting['key'] . ' existed, ignore...');
            } else {
                $result = DB::table('settings')->insert($setting);
    
                if (!$result) {
                    $this->command->info("Insert failed at record $index.");
    
                    return;
                }
            }
        }
    
        $this->command->info('Inserted '.count($this->settings).' records.');
    }
    

    关于作者 🌱

    我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊,或者关注我的个人公众号“大象工具”, 查看更多联系方式