Seeders 本文未发布 发布文章

未匹配的标注

Definition

Seeders (are a short name for Database Seeders).
Seeders are classes made to seed the database with real data, this data usually should exist in the Application after the installation (Example: the default Users Roles and Permissions or the list of Countries).

Principles

  • Seeders SHOULD be created in the Containers. (If the container is using a package that publishes a Seeder class, this class should be manually placed in the Container that make use of it. Do not reply on the package to place it on its right location).

Rules

  • Seeders should be in the right directory inside the container to be loaded.
  • To avoid any conflict between containers seeders classes, you SHOULD always prepend the Seeders of each container with the container name. (Example: UserPermissionsSeeder,ItemPermissionsSeeder). If 2 seeders classes have the same name but live in different containers, one of them will not be loaded.
  • If you wish to order the seeding of the classes, you can just append _1, _2 to your classes.

Folder Structure

 - App
    - Containers
        - {container-name}
             - Data
                - Seeders
                    - ContainerNameRolesSeeder_1.php
                    - ContainerNamePermissionsSeeder_2.php
                    - ...

Code Samples

Roles Seeder:

<?php

namespace App\Containers\Order\Data\Seeders;

use App\Ship\Parents\Seeders\Seeder;
use Apiato\Core\Foundation\Facades\Apiato;

class OrderPermissionsSeeder_1 extends Seeder
{

    public function run()
    {
        Apiato::call('Authorization@CreatePermissionTask', ['approve-reject-orders']);
        Apiato::call('Authorization@CreatePermissionTask', ['find-orders']);
        Apiato::call('Authorization@CreatePermissionTask', ['list-orders']);
        Apiato::call('Authorization@CreatePermissionTask', ['update-orders']);
        Apiato::call('Authorization@CreatePermissionTask', ['delete-orders']);

        // ...

    }
}

Note: Same Seeder class is allowed to contain seeding for multiple Models.

Run the Seeders

After registering the Seeders you can run this command:

php artisan db:seed

To run specific Seeder class you can specific its class in the parameter as follow:

php artisan db:seed --class="your\single\seeder\goes-here"

Migrate & seed at the same time

php artisan migrate --seed

For more information about the Database Seeders read this.

Apiato testing seeder command

It’s useful sometimes to create a big set of testing data. apiato facilitates this task:

  1. Open app/Ship/Seeders/SeedTestingData.php and write your testing data here.
  2. Run this command any time you want this data available (example at staging servers):
php artisan apiato:seed-test

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 0
发起讨论 只看当前版本


暂无话题~