#简介
Laravel includes a simple method of seeding your database with test data using seed classes. All seed classes are stored in database/seeds
. Seed classes may have any name you wish, but probably should follow some sensible convention, such as UserTableSeeder
, etc. By default, a DatabaseSeeder
class is defined for you. From this class, you may use the call
method to run other seed classes, allowing you to control the seeding order.
Laravel 引入了一个简单方法用测试数据来填充你的数据库, 所有的数据填充类(seed classes)都放置在 database/seeds路径下, 数据库填充类可以有任何你希望的名字, 但是最好有一定的规范,比如UserTableSeeder, 等。默认, 一个 DatabaseSeeder 类给你定义好,从这个类,你可以使用call方法来运行其他填充类, 允许你控制填充的顺序。
#编写数据填充类
To generate a seeder, you may issue the make:seeder
Artisan command. All seeders generated by the framework will be placed in the database/seeders
directory:
要生成一个填充类你可以执行一个 make : seeder Artisan 命令。 所有的框架生成的填充类都放置在 database/seeders目录下
php artisan make:seeder UserTableSeeder
A seeder class only contains one method by default: run
. This method is called when the db:seed
Artisan commandis executed. Within the run
method, you may insert data into your database however you wish. You may use thequery builder to manually insert data or you may use Eloquent model factories.
As an example, let's modify the DatabaseSeeder
class which is included with a default installation of Laravel. Let's add a database insert statement to the run
method:
一个填充类seeder 默认只包含一个run方法, 当执行db: seed Artisan 命令的时候就会被执行。 这个run方法里面,你如愿可以插入数据到数据库,你可以使用查询构造器手动插入数据,或使用 Eloquent 模型工厂。
作为一个例子,让我们Laravel修改附带安装的DatabaseSeeder类,我们添加数据库插入语句到run方法。
<?php
use DB;
use IlluminateDatabaseSeeder;
use IlluminateDatabaseEloquentModel;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('users')->insert([
'name' => str_random(10),
'email' => str_random(10).'@gmail.com',
'password' => bcrypt('secret'),
]);
}
}
使用模型工厂
Of course, manually specifying the attributes for each model seed is cumbersome. Instead, you can use model factories to conveniently generate large amounts of database records. First, review the model factory documentation to learn how to define your factories. Once you have defined your factories, you may use the factory
helper function to insert records into your database.
For example, let's create 50 users and attach a relationship to each user:
当然,手动为每个模型填充指定属性比较累赘,相反,你可以使用模型工厂方便地产生大量的数据记录。首先,先看看模型工厂文档来学习怎么样定义你的工厂,一旦你定义了工厂,你就可以使用工厂辅助类方法去插入数据到数据库。
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory('AppUser', 50)->create()->each(function($u) {
$u->posts()->save(factory('AppPost')->make());
});
}
调用额外的填充类
Within the DatabaseSeeder
class, you may use the call
method to execute additional seed classes. Using the call
method allows you to break up your database seeding into multiple files so that no single seeder class becomes overwhelmingly large. Simply pass the name of the seeder class you wish to run:
在DatabaseSeeder类中你可以使用call方法去执行额外的填充类,使用call方法允许你打散你的数据库填充到多个文件,这样单个填充类不会过于庞大,只需简单的把填充类的名字传给call方法
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
$this->call('UserTableSeeder');
$this->call('PostsTableSeeder');
$this->call('CommentsTableSeeder');
}
#运行填充
Once you have written your seeder classes, you may use the db:seed
Artisan command to seed your database. By default, the db:seed
command runs the DatabaseSeeder
class, which may be used to call other seed classes. However, you may use the --class
option to specify a specific seeder class to run individually:
一旦你写好了填充类,你就可以用db: seed Artisan 命令来填充数据库,默认db: seed 命令运行DatabaseSeeder类,它用来调用其它填充类。 然而,你可以使用 --class选项来定义一个指定的填充类来单独运行:
php artisan db:seed
php artisan db:seed --class=UserTableSeeder
You may also seed your database using the migrate:refresh
command, which will also rollback and re-run all of your migrations. This command is useful for completely re-building your database:
你也可以使用 migrate:refresh 命令填充数据库,可以回滚和重新运行所有你的迁移。 这个命令对于重建你的数据库是十分有用的。
php artisan migrate:refresh --seed