laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
31.85k stars 10.79k forks source link

How to seed database after deploying the Laravel app on production Heroku? #14130

Closed balodoc closed 8 years ago

balodoc commented 8 years ago

I successfully deployed a laravel in production in Heroku but when I run 'heroku run php artisan db:seed' I got this error:

[Symfony\Component\Debug\Exception\FatalThrowableError] Fatal error: Class 'Faker\Factory' not found

Does any one of you get the error deploying on heroku?

I use cleardb add ons.

When I run 'heroku run php artisan migrate' in production it all works fine only the heroku run php artisan db:seed' has error. It all works on development side, but in production I got the error.

It seems like in the Faker library id not loaded in the DatabaseSeeder class? Is there a problem on my composer not loading the Faker library? or permission on vendor folder?

I will appreciate any help. Thanks.

ragmon commented 7 years ago

Need help with this too!

canmanvan commented 7 years ago

This is very strange. I spend 6 hours in finding that issue but unable to resolve that. I also deployed it on other servers but only heroku is not reading the faker class. Even I add the "fzaninotto/faker" in the require section in composer.json. Any one have done it. Please reply. [Symfony\Component\Debug\Exception\FatalThrowableError]
Class 'Faker\Factory' not found

canmanvan commented 7 years ago

@rolandtacadena How do you find the solution of this problem?

Carlos-H-Alves commented 7 years ago

in composer.json change : "fzaninotto/faker" of "require-dev" for "require"

balodoc commented 7 years ago

Thanks @Carlos-Alves !

RoseEnd commented 6 years ago

@Carlos-Alves I use your method but don't solve problem, why? "require": { "php": ">=7.0.0", "fideloper/proxy": "~3.3", "laravel/framework": "5.5.*", "laravel/tinker": "~1.0", "overtrue/laravel-lang": "~3.0", "fzaninotto/faker": "~1.4" }, "require-dev": { "filp/whoops": "~2.0", "mockery/mockery": "0.9.*", "phpunit/phpunit": "~6.0" },

balodoc commented 6 years ago

​Hi @RoseEnd in your project folder login to heroku, run heroku run bash

balodoc commented 6 years ago

​Hi @RoseEnd in your project folder login to heroku: heroku login in composer.json change : "fzaninotto/faker" of "require-dev" for "require" commit to your heroku branch and push: git push heroku master or if you use other branch name other than "master" git push heroku your_branch:master run heroku run bash then run composer install

Then run php artisan migrate:refresh --seed --force and lets see.

On Wed, Aug 23, 2017 at 1:12 PM, Roland Tacadena tacadenaroland@gmail.com wrote:

​Hi @RoseEnd in your project folder login to heroku, run heroku run bash

-- Roland C. Tacadena

4sh3 commented 6 years ago

I did it but i️s not workin... :(

  1. I logued into heroku and I upgrade all the code that is working instead of the db:seed.
  2. I changed the composer.json:
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=7.0",
        "appstract/laravel-opcache": "^1.2",
        "artesaos/seotools": "^0.10.0",
        "backup-manager/laravel": "^1.2",
        "filp/whoops": "^2.1",
        "laravel/framework": "5.5.*",
        "laravel/tinker": "~1.0",
        "laravelcollective/html": "^5.3.0",
        "league/flysystem-aws-s3-v3": "^1.0",
        "league/flysystem-sftp": "^1.0",
        "spatie/laravel-sitemap": "^3.1",
        "srmklive/flysystem-dropbox-v2": "^1.0",
        "uxweb/sweet-alert": "^1.4",
        "yajra/laravel-datatables-oracle": "^8.0",
        "fzaninotto/faker": "~1.4"
    },
    "require-dev": {
        "barryvdh/laravel-debugbar": "^3.0",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~6.0"
    },
    "autoload": {
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-root-package-install": [
            "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ],
        "post-install-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postInstall",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "php artisan optimize"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    }
}
git add .
git commit -am "changes"
git push heroku master
...
Migrations work successfully but db:seed fail:

heroku php artisan db:seed

--> FAIL: 
[Symfony\Component\Debug\Exception\FatalThrowableError]
  Class 'Faker\Factory' not found

Folders:

Model:


$factory->define(App\User::class, function (Faker\Generator $faker) {
    static $password;

    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'password' => $password ?: $password = bcrypt('secret'),
        'remember_token' => str_random(10),
    ];
});

DatabaseSeeders:

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // php artisan migrate:refresh --seed
        $this->call(UsersTableSeeder::class);

        factory(App\Log::class, 100)->create();
        factory(App\Tag::class, 10)->create();
        factory(App\Post::class, 10)->create();
        factory(\App\Maintenance::class, 1)->create();
        factory(\App\Post_tag::class, 20)->create();
    }
}
chaijiawei commented 6 years ago
  1. "fzaninotto/faker" of "require-dev" for "require"
  2. git push heroku master
  3. heroku run bash
  4. composer install
  5. php artisan db:seed

repeat steps 3 to 5 each time when you want to db:seed

densityx commented 6 years ago
  1. move "fzaninotto/faker" from "require-dev" to "require"
  2. update composer with composer update (this will update composer.lock file)
  3. push it to github / deploy to heroku
llxcyzgh commented 5 years ago
"fzaninotto/faker" of "require-dev" for "require"
git push heroku master
heroku run bash
composer install
php artisan db:seed

this really works

fredpen commented 4 years ago

If for any reason you are still having issue with this

Do the following

phyothiha commented 4 years ago

I follow the steps and failed at composer update. It says "PHP Fatal error: Allowed memory size of # bytes exhausted".

js-moreno commented 4 years ago

Simply execute in your Heroku console

composer install --dev to install require-dev dependencies

php artisan db:seed to run your seeders

elliottpost commented 3 years ago

Alternatively, if you do not have an NAT Gateway protecting your database and you can access it by IP, you can just change your local .env file to the DB creds from Vapor (see below for example) and then run php artisan db:seed to run your app locally and seed your remote database. If you're running Laravel via sail, then the command would be sail artisan db:seed.

Don't forget to change your .env back to point to your local database after, if necessary.

Sample .env:

DB_CONNECTION=mysql
DB_HOST={{host}}
DB_PORT=3306
DB_DATABASE=vapor
DB_USERNAME=vapor
DB_PASSWORD={{pass}}