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.

	DB::table('subjects')->insert([
		'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.

   $this->call(SubjectsTableSeeder::class);

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.

8 thoughts on “How to use Laravel Database Seeding?

  1. hi!,I really like your writing so a lot! share we be in contact extra about your article on AOL?
    I need an expert on this house to resolve my
    problem. Maybe that is you! Taking a look forward to look you.

    Keep up the good work!

  2. I think this is among the most vital info for me. And i am glad
    reading your article. But want to remark on few general things, The website style is
    ideal, the articles is really great : D. Good job, cheers

Leave a Reply

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