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
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
That’s it!. Please share your thoughts or suggestions in the comments below.
I like your post. I shared your website on my Tumblr.
Hope that my followers are going to fancy your writing too.
Keep up the good work.
Thanks Andreev…
One of my most lіked web blogs to browse when starting the day with a drink
of coffee !
Thanks Bezrukov..
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!
I am really happy to read this web site posts which carries tons of valuable data, thanks for providing such data.
Keep up the good work!
Hello colleagues, how is everything, and what you wish
for to say about this post, in my view its truly awesome for me.
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