Monday, March 26, 2018

Laravel seeding via SQL file - development

Laravel seeding via SQL file

I’m trying to read data from a .sql file in a seeder to fill 3-4 tables with some data and DatabaseSeeder.php looks like this

public function run() {
    $this->call([
        UsersTableSeeder::class,
        // Bunch of seeders using Eloquent
        SqlSeeder::class
    ]);
}

All other seeders execute and, actually, when trying to throw an exception in SqlSeeder.php I’m able to stop the seeding. However, SqlSeeder.php won’t seed the database via php artisan migrate:fresh --seed, seems like it’s bypassed. I always need to run php artisan db:seed --class SqlSeeder after, in order to make it seed the database. SqlSeeder.php looks like this

public function run() {
    $path = base_path().'/database/seeds/sql/data.sql';
    $sql = file_get_contents($path);
    DB::unprepared($sql);
}

Why’s that?



from Laravel Questions and Answers https://laravelquestions.com/php/laravel-seeding-via-sql-file/
via Lzo Media

No comments:

Post a Comment