Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel seed after migrating

Is there something I can put in my migrations to automatically seed the table with test data once the migration has completed?

Or do you have to seed separately?

like image 611
imperium2335 Avatar asked Feb 11 '15 20:02

imperium2335


People also ask

What is migration and seeding in Laravel?

Introduction Migration and SeedingMigrations are like version control for your database, allowing your team to easily modify and share the application's database schema. Migrations are typically paired with Laravel's schema builder to easily build your application's database schema.

What does php artisan migrate fresh -- seed do?

php artisan migrate:fresh --seed executes the migrate:fresh command, but then it seeds the database. We can use this command when we install the application on a new host, so that it seeds (i.e., inserts data) into the database.

How do I roll back a seeder in Laravel?

use Undo Seeder for Laravel. When you install UndoSeeder, the following artisan commands are made available: db:seed-undo Undo seeds in the seeds directory. db:seed-refresh Undo seeds run seeds again.

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 migrate:refresh with the --seed option to automatically seed after the migrations are complete:

php artisan migrate:refresh --seed 

This will rollback and re-run all your migrations and run all seeders afterwards.


Just as a little extra, you can also always use Artisan::call() to run an artisan command from inside the application:

Artisan::call('db:seed'); 

or

Artisan::call('db:seed', array('--class' => 'YourSeederClass')); 

if you want specific seeder class.

like image 94
lukasgeiter Avatar answered Sep 26 '22 20:09

lukasgeiter


If you don't want to delete existing data and want to seed after migrating

lukasgeiter's answer is correct for test data, but running following artisan command

php artisan migrate:refresh --seed 

in production will refresh your database removing any data entered or updated from frontend.

If you want to seed your database along a migration (example rolling out an update to your application keeping existing data), like adding a new table countries along with seed data, you can do the following:

Create a database seeder example YourSeeder.php in database/seeds and your location table migration

class YourTable extends Migration {     /**      * Run the migrations.      *      * @return void      */     public function up()     {         Schema::create('tablename', function (Blueprint $table) {             $table->increments('id');             $table->string('name',1000);             $table->timestamps();             $table->softDeletes();         });          $seeder = new YourTableSeeder();         $seeder->run();     }      /**     * Reverse the migrations.     *     * @return void     */     public function down()     {         Schema::dropIfExists('tablename');     } } 

Run composer dump-autoload if there is a php class not found error for YourTableSeeder class.

like image 26
Manpreet Avatar answered Sep 25 '22 20:09

Manpreet