4

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.)

4
  • Seeding in production environment is "secured" I guess, have you tried running your artisan command via CLI in a production environment? Commented Mar 7, 2018 at 18:30
  • This is kind of a staging environment, so that, I do not have shell access to my production server. :( Commented Mar 7, 2018 at 18:32
  • Did you tried to check your log file in your stg environment? Commented Mar 7, 2018 at 18:43
  • Yes and there is nothing in it. Commented Mar 7, 2018 at 18:44

1 Answer 1

7

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]);

Sign up to request clarification or add additional context in comments.

3 Comments

Great technical answer, but IMO the real answer here is probably "gah! don't do that!" I wouldn't want a Google crawl of my site to inadvertently seed it with a bunch of data.
Completely agree @ceejayoz it's generally a bad practice to do this in production, but can't say much until we know more. Perhaps there's a middleware to avoid only certain "admin" role users to do this. Or perhaps, this route is IP whitelisted for a certain other app to call under "critical" circumstances
Thanks and I'll try this solution tonight and come back. (Y)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.