Search code examples
phplaravellaravel-artisanlaravel-5.6

Laravel programatically calling artisan command from web.php in production


I have below route:

Route::get('/beneficiaries/seed', function () {
    echo "<p>Database seeding started...</p>";
    $exitCode = Artisan::call('db:seed');
    echo "<p>Database seeding completed.</p>";
});

In my local environment, when I visit '/beneficiaries/seed', it seeds the database. But if I do the same in production, it doesn't. I just copied the seeder classes and route file.

DatabaseSeeder:

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        $this->call(BeneficiariesTableSeeder::class);
    }
}

BeneficiariesTableSeeder:

class BeneficiariesTableSeeder extends Seeder
{
    public function run()
    {
        //seeding logic...
    }
}

Why my production Artisan command doesn't get executed? (I haven't used database transaction. Even w/o it, local db gets seeded since there is no err is raised.)


Solution

  • When you run php artisan db:seed in production, there is a warning that asks you whether you're sure to seed the database in production.

    This warning confirmation in production is the reason why Artisan::call('db:seed') isn't working in production.

    To circumvent the warning, you can use the --force flag like so: php artisan db:seed --force.

    Solution

    To do the same in code, use Artisan::call('db:seed', ['--force' => true]);