Laravel 8 Tutorial - Seeding

In this tutorial, we will learn about database seeding in Laravel.

Laravel includes a simple method of seeding your database with test data using seed classes.

Seed classes may have any name, and by default, a Database Seeder class is defined for you.

From this class, we can use the call method to run other seed classes and also allowing us to control the seeding order.

So, let's create a Seeder. For that, switch to the command prompt and type:


php artisan make:seeder PostTableSeeder

Here, "PostTableSeeder" is the seeder name. This PostTableSeeder is now created inside the seeds directory.

Let's check. So, go to the project and click on database, then seeds. You can see here PostTableSeeder.

Inside the PostTableSeeder class, only one method is defined by default, which is the run method.

This method is called when the db:seed artisan command is executed. Within the run method, we can write code for inserting data into the database.

Let's modify the PostTableSeeder class and add a database insert statement inside the run method:


DB::table(posts')->insert([
'title' => 'Fitst post',
'body' => 'First post description',
]);

Also, import DB before the class:


use Illuminate\Support\Facades\DB;

Now, save the file. Now, call this Post Seeder class inside the DatabaseSeeder run method, so just type:


public function run()
{
$this->call([
PostsTableSeeder::class,
]);
}

Let's run this Seeder. Before executing seeders, regenerate Composer's autoload. So, just go to the command prompt and run the command:


composer dump-autoload

Now, we can use the db:seed Artisan command to seed the database.

By default, the db:seed command runs the DatabaseSeeder class, which may be used to call other seed classes.

Before using this seed artisan command, let's check the posts table. So, go to phpMyAdmin and here you can see inside the posts table, there is no record.

Now, execute the db:seed artisan command in the command prompt:


php artisan db:seed

Now, the command is executed. So, let's check the posts table. Alright, now you can see the record.

Now, let's seed one more record. Go to the PostTableSeeder. Add here, just change the post title and body.

Now, inside the command prompt:


php artisan db:seed

Now, go to phpMyAdmin and refresh the posts table. You can see another record.

We can also seed multiple records at a time, so just go to the PostSeeder. And here, I am going to use the Faker package, so before the class, import the Faker:


use Faker\Factory as Faker;

Inside the run method, just type:


public function run() {
$faker = Faker::create();
foreach(range(1, 100) as $index) {
DB::table(posts')->insert([
'title' => $faker->sentence(5),
'content' => $faker->paragraph(4)
]);
}
}

Now, run this Seeder. For that, go to the command prompt and here type:


php artisan db:seed

Let's check the post table, go to phpMyAdmin. And just refresh the posts table. You can see here hundreds of records are inserted here.

You may also seed your database using the migrate:fresh command, which will drop all tables and re-run all of your migrations.

This command is useful for completely re-building your database. So, let's check this command:

Go to the command prompt and here type:


php artisan migrate:fresh –seed

Now, you can see here all tables are recreated, and you can also see the records are re-inserted inside the post table.

So, in this way, you can use the database seeder in Laravel 8.