data filling
- Call other Seeders
- Running SeedersLaravel includes a filler class that can fill your database with test data. All filling classes are placed in the database/seeds
- directory. You can name the filler class whatever you want, but it is recommended that you follow a naming convention like UsersTableSeeder. Usually, Laravel defines a DatabaseSeeder
call method to run other seed classes to control the order of data filling.
Writing Seeders
Run make:seeder
this Artisan command to generate Seeder. The seeders generated by the framework will be placed in the database/seeds
directory.
php artisan make:seeder UsersTableSeeder
A seeder class only contains one default method: run
. This method will be called when executing the db:seed
Artisan command. In the run
method you can insert data into the database as needed. You can also use the query builder or Eloquent model factory to insert data manually.
{tip} Batch assignment protection is automatically disabled when data is populated.
As shown below, add a data insertion statement in the run
method in the default DatabaseSeeder
class:
<?php use Illuminate\Support\Str; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; 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'), ]); } }
{tip} You can use types to constrain the dependencies you need in the method signature of
run
. They are automatically resolved by the Laravel service container.
Using Model Factory
Of course, manually populating specified properties for each model is cumbersome. As an alternative, you can use a model factory to easily generate large amounts of database data. First, read the model factory documentation to learn how to define a factory file. Once you have defined your factory file, you can then use the factory
helper function to insert data into the database.
For example, create 50 users and create an association for each user:
/** * 运行数据库填充。 * * @return void */ public function run(){ factory(App\User::class, 50)->create()->each(function ($u) { $u->posts()->save(factory(App\Post::class)->make()); }); }
Call other Seeders
In the DatabaseSeeder
class, you can use the call
method to run other seed classes. Use the call
method to split the data population into multiple files so that a single seeder does not become extremely large. Simply pass the name of the seeder class you want to run:
/** * 运行数据库 seeds。 * * @return void */ public function run(){ $this->call([ UsersTableSeeder::class, PostsTableSeeder::class, CommentsTableSeeder::class, ]); }
Running Seeders
Complete the seeder class After writing, you may need to regenerate Composer's autoloader using the dump-autoload
command:
composer dump-autoload
Now you can use the Artisan command db:seed
to populate the database . By default, the db:seed
command will run the DatabaseSeeder
class, which can be used to call other Seed classes. However, you can also use the --class
option to specify a specific seeder class:
php artisan db:seed php artisan db:seed --class=UsersTableSeeder
You can also use the migrate:refresh
command to populate the database, This command rolls back and reruns all migrations. This command can be used to rebuild the database:
php artisan migrate:refresh --seed