collective-thinking / laravel-seed
像运行迁移一样运行种子。
Requires
- php: >=8.1.0
- laravel/framework: 8.*|9.*|10.*
Requires (Dev)
- larastan/larastan: ^2.0
- laravel/pint: 1.13.10
- nunomaduro/collision: 7.x
- orchestra/testbench: 8.x
- phpstan/phpstan: 1.10.57
- phpunit/phpunit: ^10.5.0
This package is auto-updated.
Last update: 2024-09-08 18:00:52 UTC
README
像运行迁移一样运行种子。
摘要
关于
我创建了此包,因为我正在处理预先填充数据的表(如性别表、产品类型表等)。
我发现了一系列的包,laravel-seeder,似乎被多次fork,但这些包要么无法正确安装,要么分支真的过时。
我决定从头开始创建此包,目标是使其尽可能接近官方迁移体验。
在这种方法中,每个种子器都是一个类,就像迁移一样:它定义了“up”和“down”方法,因此您可以运行和回滚您的种子。像迁移一样,文件的顺序由创建时间的时间戳决定。
我使用此包的情况是在生产中填充我的应用程序,为了自动化此过程,无需逐个运行每个种子类,也无需手动操作。
先决条件
- PHP >= 8.1.0
- Laravel >= 8.0.0
安装
在您的根项目文件夹中,运行此命令
composer require collective-thinking/laravel-seed
对于较旧的Laravel版本,您需要在config/app.php
文件的“Providers”键中注册服务提供者,如下所示
'providers' => [ // ... CollectiveThinking\LaravelSeed\LaravelSeedServiceProvider::class, ]
使用
创建一个新的种子器
如“关于”部分中所述,种子器是类级别的。它们定义了up和down方法,并且是使用命令行种子的唯一方法。
使用Artisan命令
要创建一个新的种子器,请使用以下Artisan命令行
php artisan seed:make InsertPostCategory
这将创建一个名为database/seeders/2020_07_24_094613_insert_post_categories.php
的新种子器类。如果您打开文件,它看起来是这样的
class InsertPostCategories { public function up() { // } public function down() { // } }
指定Eloquent模型
通常,种子器会填充由您的Eloquent模型之一建模的表。
要指定您的种子器将通过哪个Eloquent模型填充数据,您可以使用以下--model
参数
php artisan seed:make InsertPostCategory --model=PostCategeory
您的种子器内容将包含您通常需要编写的常用样板代码
use App\PostCategory; class InsertPostCategories { public function up() { PostCategory::insert([ [ "id" => 1, ], ]); } public function down() { PostCategory::destroy([ 1, ]); } }
检查种子的状态
如果您需要有关哪些种子已运行或尚未运行的报告,您可以使用以下Artisan命令
php artisan seed:status
这将显示一个表格,其中每行指定了种子器的状态。
$ php artisan seed:status +------------------------------------------+---------+ | file | status | +------------------------------------------+---------+ | 2020_07_24_094613_insert_post_categories | not ran | +------------------------------------------+---------+ 1 row(s) displayed.
运行种子器
当您的种子器准备就绪时,您可以使用此命令运行尚未运行的任何种子器。
php artisan seed
这将显示一个表格,总结哪些种子器已成功运行。
$ php artisan seed 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100% +------------------------------------------+ | file | +------------------------------------------+ | 2020_07_24_094613_insert_post_categories | +------------------------------------------+ 1 seed(s) ran.
回滚种子
回滚种子可能有助于测试一切是否正常。有两种回滚种子的方式。
回滚所有内容
如果您确定需要删除所有种子,您可以使用以下命令
php artisan seed:reset
这将按相反的顺序取回每个种子,并运行它们的down()
方法。
$ php artisan seed:reset 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100% +------------------------------------------+ | file | +------------------------------------------+ | 2020_07_24_094613_insert_post_categories | +------------------------------------------+ 1 seed(s) rollbacked.
回滚最后一批
当您运行php artisan seed
时,它会将“批次”号与当时运行的种子相关联。
让我们假设您正在创建这个博客文章网络应用程序。您需要填充一些帖子类别,因此您一次运行php artisan seed
。
此包将把批次号“1”与这个第一批种子相关联。
然后,您需要建模作者性别和作者职位(初级或高级)。因此,您创建 Gender
和 Position
模型、种子文件,然后再次运行 php artisan seed
。
这样就会将这些附加种子与批次号 "2" 关联。
到目前为止,我们已经存储了以下内容
+------------------------------------------+--------------+ | 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 | +------------------------------------------+--------------+
因此,当您运行 php artisan seed:rollback
时,这将会依次回滚 insert_positions
和 insert_genders
,因为它们属于同一批次。
$ php artisan seed:rollback 2/2 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100% +------------------------------------+ | file | +------------------------------------+ | 2020_07_25_065903_insert_genders | | 2020_07_25_075926_insert_positions | +------------------------------------+ 2 seed(s) rollbacked.
然后,如果您再次运行 php artisan seed:rollback
,这将单独回滚 insert_post_categories
种子。
$ php artisan seed:rollback 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100% +-----------------------------------------+ | file | +-----------------------------------------+ | 2020_07_24_094613_insertpost_categories | +-----------------------------------------+ 1 seed(s) rollbacked.