承接 khalyomede/laravel-seed 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

khalyomede/laravel-seed

Composer 安装命令:

composer require khalyomede/laravel-seed

包简介

Run your seeds like you run your migrations.

README 文档

README

Run your seeds like you run your migrations.

Packagist License Packagist Version Maintainability

laravel-seed-showcase-2

Summary

About

I created this package because I am working with tables that are pre-filled with data (like a Gender table, a Product type table, and so on).

I found a series of package, laravel-seeder, which seemed to have been forked a lot of times, but either the packages would not install properly, or the forks were really outdated.

I decided to take a fresh start and create this package from scratch, with having in mind to be as close as the official migration experience.

In this approach, each seeders is a class, like a migration: it defines both "up" and "down" methods, so you can run and rollback your seeds. Like migrations, the order of the files is determined by a timestamp at the creation time.

My use case for this package is to populate my apps in production, in order to automatize this process, without having to deal with running each seed class individually, and manually.

Prerequisites

  • PHP >= 7.0.0
  • Laravel >= 5.5.0

Installation

In your root project folder, run this command:

composer require khalyomede/laravel-seed

For older Laravel versions, you will need to register the service provider in the key "Providers" of the config/app.php file like following:

'providers' => [
  // ...
  Khalyomede\LaravelSeed\LaravelSeedServiceProvider::class,
]

Usage

Create a new seeder

Like mentioned in the about section, seeders are class-based. They define a up and down method, and are the only way to use the command lines to seed your database.

Using the artisan command

To create a new seeder, use this Artisan command line:

php artisan seed:make InsertPostCategory

This will create a new seeder class at database/seeders/2020_07_24_094613_insert_post_categories.php. If you open the file, this is how it looks:

class InsertPostCategories
{
  public function up()
  {
    //
  }

  public function down()
  {
    //
  }
}

Specifying an Eloquent model

In general seeders will populate a table modelized by one of your Eloquent model.

To specify through which Eloquent model your seeder will fill data, you can specify the --model argument like following:

php artisan seed:make InsertPostCategory --model=PostCategeory

The content of your seeder will be filled with the usual boilerplate code you would have written:

use App\PostCategory;

class InsertPostCategories
{
  public function up()
  {
    PostCategory::insert([
      [
        "id" => 1,
      ],
    ]);
  }

  public function down()
  {
    PostCategory::destroy([
      1,
    ]);
  }
}

Checking the status of your seeds

If you need to have a report of which seeds have been ran or not yet, you can use the following Artisan command:

php artisan seed:status

This will prompt a table, with each lines specifying the status of the seeder.

$ php artisan seed:status

+------------------------------------------+---------+
| file                                     | status  |
+------------------------------------------+---------+
| 2020_07_24_094613_insert_post_categories | not ran |
+------------------------------------------+---------+

1 row(s) displayed.

Running seeders

When your seeders is ready, you can use this command to run any seeders that have not been ran yet.

php artisan seed

This will also prompt a table summarizing which seeders have been successfuly run.

$ php artisan seed

 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

+------------------------------------------+
| file                                     |
+------------------------------------------+
| 2020_07_24_094613_insert_post_categories |
+------------------------------------------+

1 seed(s) ran.

Rollbacking seeds

Rollbacking seeds can be useful to test again that everything goes right. There is two way to rollback seeds.

Rollbacking everything

If you are sure you need to remove every seeds, you can use the following command:

php artisan seed:reset

This will take every seeds in reverse order, and run their down() method.

$ php artisan seed:reset

 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

+------------------------------------------+
| file                                     |
+------------------------------------------+
| 2020_07_24_094613_insert_post_categories |
+------------------------------------------+

1 seed(s) rollbacked.

Rollbacking the last batch

When you run php artisan seed, it associate a "batch" number to the seeds that have been run at this time.

Let's imagine you are creating this blog post web app. You need to fill some post categories, so you run php artisan seed one time.

This package will associate the batch number "1" to this first seed batch.

Then, you need to modelize author genders, and author positions (junior or senior). So you create both the Gender and Position models, the seeders, and run a second time php artisan seed.

This will then associate the batch number "2" for these two additionals seeds.

At this point, this is what we have stored so far:

+------------------------------------------+--------------+
| file                                     | batch number |
+------------------------------------------+--------------+
| 2020_07_24_094613_insert_post_categories |            1 |
| 2020_07_25_065903_insert_genders         |            2 |
| 2020_07_25_075926_insert_positions       |            2 |
+------------------------------------------+--------------+

So when you run php artisan seed:rollback, this will rollback both insert_positions and insert_genders in a row, since they are in the same batch.

$ php artisan seed:rollback

 2/2 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

+------------------------------------+
| file                               |
+------------------------------------+
| 2020_07_25_065903_insert_genders   |
| 2020_07_25_075926_insert_positions |
+------------------------------------+

2 seed(s) rollbacked.

Then, if you run again php artisan seed:rollback, this will rollback the insert_post_categories seed alone.

$ php artisan seed:rollback

 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

+-----------------------------------------+
| file                                    |
+-----------------------------------------+
| 2020_07_24_094613_insertpost_categories |
+-----------------------------------------+

1 seed(s) rollbacked.

khalyomede/laravel-seed 适用场景与选型建议

khalyomede/laravel-seed 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2020 年 07 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「migration」 「laravel」 「batch」 「seed」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 khalyomede/laravel-seed 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 khalyomede/laravel-seed 我们能提供哪些服务?
定制开发 / 二次开发

基于 khalyomede/laravel-seed 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 13k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 10
  • Watchers: 0
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-07-25