How to use Laravel Database Seeding?

In this article, We’ll see how to use Laravel Database Seeding? Seed allows you to insert data into your database. We can use the query builder to manually insert data or Eloquent model factories.

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

Let’s assume we have table ‘subjects’ in the database. This table contains the columns name, author etc.

Open the command prompt or terminal in your Laravel project. To use the seed, we create a seeder which insert the data in table ‘subjects’.

  php artisan make:seeder SubjectsTableSeeder

Laravel Seeder

A seeder class only contains one method by default: run. This method is called when the db:seed Artisan command is executed.

Open the SubjectsTableSeeder.php in your text editor and add the below code in run method.

		'name' => 'Java',
		'author' => 'James Gosling',
		'created_at' => date("Y-m-d H:i:s")

By default, Laravel provides a DatabaseSeeder class. Using this class, we can use the call method to run other seed classes, allowing you to control the seeding order. Open the DatabaseSeeder.php in your text editor and add the below code in run method.


To run the seeds, execute the below code in your command prompt or terminal

   php artisan db:seed

Laravel Seeder Command

Laravel Seeder Insert

That’s it!. Please share your thoughts or suggestions in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *