Database: Seeding

Introduction

Laravel includes the ability to seed your database with data using seed classes. All seed classes are stored in the database/seeders directory. 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.

lightbulb

Mass assignment protection is automatically disabled during database seeding.

Writing Seeders

To generate a seeder, execute the make:seeder Artisan command. All seeders generated by the framework will be placed in the database/seeders directory:

1php artisan make:seeder UserSeeder
1php artisan make:seeder UserSeeder

A seeder class only contains one method by default: run. This method is called when the db:seed Artisan command is executed. Within the run method, you may insert data into your database however you wish. You may use the query builder to manually insert data or you may use Eloquent model factories.

As an example, let's modify the default DatabaseSeeder class and add a database insert statement to the run method:

1<?php
2 
3namespace Database\Seeders;
4 
5use Illuminate\Database\Seeder;
6use Illuminate\Support\Facades\DB;
7use Illuminate\Support\Facades\Hash;
8use Illuminate\Support\Str;
9 
10class DatabaseSeeder extends Seeder
11{
12 /**
13 * Run the database seeders.
14 *
15 * @return void
16 */
17 public function run()
18 {
19 DB::table('users')->insert([
20 'name' => Str::random(10),
21 'email' => Str::random(10).'@gmail.com',
22 'password' => Hash::make('password'),
23 ]);
24 }
25}
1<?php
2 
3namespace Database\Seeders;
4 
5use Illuminate\Database\Seeder;
6use Illuminate\Support\Facades\DB;
7use Illuminate\Support\Facades\Hash;
8use Illuminate\Support\Str;
9 
10class DatabaseSeeder extends Seeder
11{
12 /**
13 * Run the database seeders.
14 *
15 * @return void
16 */
17 public function run()
18 {
19 DB::table('users')->insert([
20 'name' => Str::random(10),
21 'email' => Str::random(10).'@gmail.com',
22 'password' => Hash::make('password'),
23 ]);
24 }
25}
lightbulb

You may type-hint any dependencies you need within the run method's signature. They will automatically be resolved via the Laravel service container.

Using Model Factories

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.

For example, let's create 50 users that each has one related post:

1use App\Models\User;
2 
3/**
4 * Run the database seeders.
5 *
6 * @return void
7 */
8public function run()
9{
10 User::factory()
11 ->count(50)
12 ->hasPosts(1)
13 ->create();
14}
1use App\Models\User;
2 
3/**
4 * Run the database seeders.
5 *
6 * @return void
7 */
8public function run()
9{
10 User::factory()
11 ->count(50)
12 ->hasPosts(1)
13 ->create();
14}

Calling Additional Seeders

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 too large. The call method accepts an array of seeder classes that should be executed:

1/**
2 * Run the database seeders.
3 *
4 * @return void
5 */
6public function run()
7{
8 $this->call([
9 UserSeeder::class,
10 PostSeeder::class,
11 CommentSeeder::class,
12 ]);
13}
1/**
2 * Run the database seeders.
3 *
4 * @return void
5 */
6public function run()
7{
8 $this->call([
9 UserSeeder::class,
10 PostSeeder::class,
11 CommentSeeder::class,
12 ]);
13}

Running Seeders

You may execute the db:seed Artisan command to seed your database. By default, the db:seed command runs the Database\Seeders\DatabaseSeeder class, which may in turn invoke other seed classes. However, you may use the --class option to specify a specific seeder class to run individually:

1php artisan db:seed
2 
3php artisan db:seed --class=UserSeeder
1php artisan db:seed
2 
3php artisan db:seed --class=UserSeeder

You may also seed your database using the migrate:fresh command in combination with the --seed option, which will drop all tables and re-run all of your migrations. This command is useful for completely re-building your database:

1php artisan migrate:fresh --seed
1php artisan migrate:fresh --seed

Forcing Seeders To Run In Production

Some seeding operations may cause you to alter or lose data. In order to protect you from running seeding commands against your production database, you will be prompted for confirmation before the seeders are executed in the production environment. To force the seeders to run without a prompt, use the --force flag:

1php artisan db:seed --force
1php artisan db:seed --force

Comments

No Comments Yet

“Laravel” is a Trademark of Taylor Otwell.
The source documentation is released under MIT license. See laravel/docs on GitHub for details.
The translated documentations are released under MIT license. See cornch/laravel-docs-l10n on GitHub for details.