laravel5.7如何进行数据库填充?

在以laravel框架为内核开发过程中需要进行数据的填充,以便与在开发中的调试与功能测试。laravel数据填充大体上是分成三部,分别是创建seeder文件并编辑填充脚本与执行脚本,其实主要的部分都在seeder填充数据逻辑中只要掌握了这部分的技巧那么对于以后的数据填充大有裨益。

首先创建 Seeder 文件

我们通过make:seeder命令创建一个Seeder文件:

$ php artisan make:seeder CategoriesSeeder

编辑相应的CategoriesSeeder.php文件

database/seeds/CategoriesSeeder.php
<?php

use App\Models\Category;
use Illuminate\Database\Seeder;

class CategoriesSeeder extends Seeder
{
    public function run()
    {
        $categories = [
            [
                'name'     => '手机配件',
                'children' => [
                    ['name' => '手机壳'],
                    ['name' => '贴膜'],
                    ['name' => '存储卡'],
                    ['name' => '数据线'],
                    ['name' => '充电器'],
                    [
                        'name'     => '耳机',
                        'children' => [
                            ['name' => '有线耳机'],
                            ['name' => '蓝牙耳机'],
                        ],
                    ],
                ],
            ],
            [
                'name'     => '电脑配件',
                'children' => [
                    ['name' => '显示器'],
                    ['name' => '显卡'],
                    ['name' => '内存'],
                    ['name' => 'CPU'],
                    ['name' => '主板'],
                    ['name' => '硬盘'],
                ],
            ],
            [
                'name'     => '电脑整机',
                'children' => [
                    ['name' => '笔记本'],
                    ['name' => '台式机'],
                    ['name' => '平板电脑'],
                    ['name' => '一体机'],
                    ['name' => '服务器'],
                    ['name' => '工作站'],
                ],
            ],
            [
                'name'     => '手机通讯',
                'children' => [
                    ['name' => '智能机'],
                    ['name' => '老人机'],
                    ['name' => '对讲机'],
                ],
            ],
        ];

        foreach ($categories as $data) {
            $this->createCategory($data);
        }
    }

    protected function createCategory($data, $parent = null)
    {
        // 创建一个新的类目对象
        $category = new Category(['name' => $data['name']]);
        // 如果有 children 字段则代表这是一个父类目
        $category->is_directory = isset($data['children']);
        // 如果有传入 $parent 参数,代表有父类目
        if (!is_null($parent)) {
            $category->parent()->associate($parent);
        }
        //  保存到数据库
        $category->save();
        // 如果有 children 字段并且 children 字段是一个数组
        if (isset($data['children']) && is_array($data['children'])) {
            // 遍历 children 字段
            foreach ($data['children'] as $child) {
                // 递归调用 createCategory 方法,第二个参数即为刚刚创建的类目
                $this->createCategory($child, $category);
            }
        }
    }
}

其次执行数据库填充

这一步是将我们创建的Seeder填充到数据库中,通过 --class 参数可以指定执行的Seeder文件:

$ php artisan db:seed --class=CategoriesSeeder

再次查看一下效果

idnameparent_idis_directorylevelpathcreated_atupdated_at
1手机配件null10-2019-03-07 03:53:562019-03-07 03:53:56
2手机壳301-3-2019-03-07 03:53:562019-03-07 03:53:56
3贴膜301-3-2019-03-07 03:53:562019-03-07 03:53:56
4存储卡301-3-2019-03-07 03:53:562019-03-07 03:53:56
5数据线301-3-2019-03-07 03:53:562019-03-07 03:53:56
6充电器301-3-2019-03-07 03:53:562019-03-07 03:53:56
7耳机311-3-2019-03-07 03:53:562019-03-07 03:53:56
8有线耳机922-3-9-2019-03-07 03:53:562019-03-07 03:53:56
9蓝牙耳机922-3-9-2019-03-07 03:53:562019-03-07 03:53:56
10电脑配件null10-2019-03-07 03:53:562019-03-07 03:53:56
11显示器1210-12-2019-03-07 03:53:562019-03-07 03:53:56
12显卡1201-12-2019-03-07 03:53:562019-03-07 03:53:56
13内存1201-12-2019-03-07 03:53:562019-03-07 03:53:56
14CPU1201-12-2019-03-07 03:53:562019-03-07 03:53:56
15主板1201-12-2019-03-07 03:53:562019-03-07 03:53:56
16硬盘1201-12-2019-03-07 03:53:562019-03-07 03:53:56
17电脑整机null10-2019-03-07 03:53:562019-03-07 03:53:56
18笔记本1901-19-2019-03-07 03:53:562019-03-07 03:53:56
19台式机1901-19-2019-03-07 03:53:562019-03-07 03:53:56
20平板电脑1901-19-2019-03-07 03:53:562019-03-07 03:53:56
21一体机1901-19-2019-03-07 03:53:562019-03-07 03:53:56
22服务器1901-19-2019-03-07 03:53:562019-03-07 03:53:56
23工作站1901-19-2019-03-07 03:53:562019-03-07 03:53:56
24手机通讯null10-2019-03-07 03:53:562019-03-07 03:53:56
25智能机2600-26-2019-03-07 03:53:562019-03-07 03:53:56
26老人机2601-26-2019-03-07 03:53:562019-03-07 03:53:56
27对讲机2601-26-2019-03-07 03:53:562019-03-07 03:53:56

这样数据填充就完成了

阅读 65

Comments