Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 db seed specific seeder file

I have an existing user table with information seeded via db:seed from UserSeeder.php. Now, I am adding new product table and want to seed information into product table. How can I prevent Laravel from seeding the UserSeeder into the database, but only the new ProductSeeder being seeded?

Thanks.

like image 812
user1995781 Avatar asked Nov 18 '13 03:11

user1995781


People also ask

How do I seed a specific seeder in Laravel?

Writing SeedersA 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.


2 Answers

You can call individual seed classes by their class name. From the docs.

By default, the db:seed command runs the DatabaseSeeder class, which may be used to call other seed classes. However, you may use the --class option to specify a specific seeder class to run individually:

php artisan db:seed --class=ProductTableSeeder 

In the example above, the ProductTableSeeder class should exist in database/seeds.

like image 184
Sajan Parikh Avatar answered Sep 20 '22 15:09

Sajan Parikh


Here's a working example with the class full namespace:

Should use double backslashes \\.

Class name is DefaultBannersSeeder.

php artisan db:seed --class=App\\Containers\\Banners\\Data\\Seeders\\DefaultBannersSeeder 
like image 21
Mahmoud Zalt Avatar answered Sep 22 '22 15:09

Mahmoud Zalt