Laravel 8.x 部署至 Heroku,执行数据库数据填充时,报错:Class “Faker\Factory” not found

1、Laravel 8.x 部署至 Heroku,执行数据库数据填充时,报错:Class “Faker\Factory” not found。如图1

图1

PS E:\wwwroot\weibo> heroku run php artisan migrate:refresh --seed
 »   Warning: heroku update available from 7.53.0 to 7.59.2.
Running php artisan migrate:refresh --seed on ⬢ app-wangqiang-weibo... up, run.5843 (Free)
**************************************
*     Application In Production!     *
**************************************

 Do you really wish to run this command? (yes/no) [no]:

Rolling back: 2021_12_24_062642_add_is_admin_to_users_table
Rolled back:  2021_12_24_062642_add_is_admin_to_users_table (18.92ms)
Rolling back: 2019_12_14_000001_create_personal_access_tokens_table
Rolling back: 2019_08_19_000000_create_failed_jobs_table
Rolled back:  2019_08_19_000000_create_failed_jobs_table (25.62ms)
Rolling back: 2014_10_12_100000_create_password_resets_table
Rolled back:  2014_10_12_100000_create_password_resets_table (10.26ms)
Rolling back: 2014_10_12_000000_create_users_table
Rolled back:  2014_10_12_000000_create_users_table (26.54ms)
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (46.62ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (17.96ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (29.18ms)
Migrating: 2019_12_14_000001_create_personal_access_tokens_table
Migrated:  2019_12_14_000001_create_personal_access_tokens_table (28.13ms)
Migrating: 2021_12_24_062642_add_is_admin_to_users_table
Migrated:  2021_12_24_062642_add_is_admin_to_users_table (7.20ms)
Seeding: Database\Seeders\UsersTableSeeder

In DatabaseServiceProvider.php line 91:

  Class "Faker\Factory" not found


2、编辑 composer.json ,将 “require-dev” 中的 “fakerphp/faker” 复制到 “require” 里面。如图2

图2

    "require": {
        "php": "^7.3|^8.0",
        "fruitcake/laravel-cors": "^2.0",
        "guzzlehttp/guzzle": "^7.0.1",
        "laravel/framework": "^8.65",
        "laravel/sanctum": "^2.11",
        "laravel/tinker": "^2.5",
        "overtrue/laravel-lang": "~4.0",
        "fakerphp/faker": "^1.9.1"
    },
    "require-dev": {
        "facade/ignition": "^2.5",
        "fakerphp/faker": "^1.9.1",
        "laravel/sail": "^1.0.1",
        "laravel/ui": "^3.0",
        "mockery/mockery": "^1.4.4",
        "nunomaduro/collision": "^5.10",
        "phpunit/phpunit": "^9.5.10"
    },

3、如果有可能影响到本地环境的程序运行,建议在 Sail 环境中执行相应命令:sail composer update。如图3

图3

wangqiang@DESKTOP-QLPK8QM:/mnt/e/wwwroot/weibo$ sail composer update
Loading composer repositories with package information
Updating dependencies
Lock file operations: 0 installs, 3 updates, 0 removals
  - Upgrading facade/ignition (2.17.2 => 2.17.3)
  - Upgrading laravel/framework (v8.76.2 => v8.77.1)
  - Upgrading laravel/ui (v3.4.0 => v3.4.1)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 0 installs, 3 updates, 0 removals
  - Downloading laravel/framework (v8.77.1)
  - Downloading facade/ignition (2.17.3)
  - Downloading laravel/ui (v3.4.1)
  - Upgrading laravel/framework (v8.76.2 => v8.77.1): Extracting archive
  - Upgrading facade/ignition (2.17.2 => 2.17.3): Extracting archive
  - Upgrading laravel/ui (v3.4.0 => v3.4.1): Extracting archive
Package swiftmailer/swiftmailer is abandoned, you should avoid using it. Use symfony/mailer instead.
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: facade/ignition
Discovered Package: fruitcake/laravel-cors
Discovered Package: laravel/sail
Discovered Package: laravel/sanctum
Discovered Package: laravel/tinker
Discovered Package: laravel/ui
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Discovered Package: overtrue/laravel-lang
Package manifest generated successfully.
78 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan vendor:publish --tag=laravel-assets --ansi --force
No publishable resources for tag [laravel-assets].
Publishing complete.
wangqiang@DESKTOP-QLPK8QM:/mnt/e/wwwroot/weibo$ sail composer dump-autoload
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: facade/ignition
Discovered Package: fruitcake/laravel-cors
Discovered Package: laravel/sail
Discovered Package: laravel/sanctum
Discovered Package: laravel/tinker
Discovered Package: laravel/ui
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Discovered Package: overtrue/laravel-lang
Package manifest generated successfully.
Generated optimized autoload files containing 5031 classes
wangqiang@DESKTOP-QLPK8QM:/mnt/e/wwwroot/weibo$

4、再次推送至 Heroku,执行填充,不再报错。如图4

图4

PS E:\wwwroot\weibo> git push heroku main
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 735 bytes | 735.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Building on the Heroku-20 stack
remote: -----> Using buildpack: heroku/php
remote: -----> PHP app detected
remote: -----> Bootstrapping...
remote: -----> Installing platform packages...
remote:        - php (8.1.1)
remote:        - ext-mbstring (bundled with php)
remote:        - composer (2.1.14)
remote:        - apache (2.4.51)
remote:        - nginx (1.20.2)
remote: -----> Installing dependencies...
remote:        Composer version 2.1.14 2021-11-30 10:51:43
remote:        Installing dependencies from lock file
remote:        Verifying lock file contents can be installed on current platform.
remote:        Package operations: 73 installs, 0 updates, 0 removals
remote:          - Downloading fakerphp/faker (v1.17.0)
remote:          - Downloading laravel/framework (v8.77.1)
remote:          - Installing doctrine/inflector (2.0.4): Extracting archive
remote:          - Installing doctrine/lexer (1.2.1): Extracting archive
remote:          - Installing symfony/polyfill-ctype (v1.23.0): Extracting archive
remote:          - Installing webmozart/assert (1.10.0): Extracting archive
remote:          - Installing dragonmantank/cron-expression (v3.1.0): Extracting archive
remote:          - Installing symfony/deprecation-contracts (v3.0.0): Extracting archive
remote:          - Installing psr/container (1.1.2): Extracting archive
remote:          - Installing fakerphp/faker (v1.17.0): Extracting archive
remote:          - Installing symfony/polyfill-php80 (v1.23.1): Extracting archive
remote:          - Installing symfony/polyfill-php73 (v1.23.0): Extracting archive
remote:          - Installing symfony/polyfill-mbstring (v1.23.1): Extracting archive
remote:          - Installing symfony/http-foundation (v5.4.1): Extracting archive
remote:          - Installing psr/event-dispatcher (1.0.0): Extracting archive
remote:          - Installing symfony/event-dispatcher-contracts (v3.0.0): Extracting archive
remote:          - Installing symfony/event-dispatcher (v6.0.1): Extracting archive
remote:          - Installing symfony/var-dumper (v5.4.1): Extracting archive
remote:          - Installing psr/log (2.0.0): Extracting archive
remote:          - Installing symfony/error-handler (v5.4.1): Extracting archive
remote:          - Installing symfony/http-kernel (v5.4.1): Extracting archive
remote:          - Installing voku/portable-ascii (1.5.6): Extracting archive
remote:          - Installing phpoption/phpoption (1.8.1): Extracting archive
remote:          - Installing graham-campbell/result-type (v1.0.4): Extracting archive
remote:          - Installing vlucas/phpdotenv (v5.4.1): Extracting archive
remote:          - Installing symfony/css-selector (v6.0.1): Extracting archive
remote:          - Installing tijsverkoyen/css-to-inline-styles (2.2.4): Extracting archive
remote:          - Installing symfony/routing (v5.4.0): Extracting archive
remote:          - Installing symfony/process (v5.4.0): Extracting archive
remote:          - Installing symfony/polyfill-php72 (v1.23.0): Extracting archive
remote:          - Installing symfony/polyfill-intl-normalizer (v1.23.0): Extracting archive
remote:          - Installing symfony/polyfill-intl-idn (v1.23.0): Extracting archive
remote:          - Installing symfony/mime (v5.4.0): Extracting archive
remote:          - Installing symfony/finder (v5.4.0): Extracting archive
remote:          - Installing symfony/polyfill-intl-grapheme (v1.23.1): Extracting archive
remote:          - Installing symfony/string (v6.0.1): Extracting archive
remote:          - Installing symfony/service-contracts (v2.4.1): Extracting archive
remote:          - Installing symfony/console (v5.4.1): Extracting archive
remote:          - Installing symfony/polyfill-iconv (v1.23.0): Extracting archive
remote:          - Installing egulias/email-validator (2.1.25): Extracting archive
remote:          - Installing swiftmailer/swiftmailer (v6.3.0): Extracting archive
remote:          - Installing symfony/polyfill-php81 (v1.23.0): Extracting archive
remote:          - Installing ramsey/collection (1.2.2): Extracting archive
remote:          - Installing brick/math (0.9.3): Extracting archive
remote:          - Installing ramsey/uuid (4.2.3): Extracting archive
remote:          - Installing psr/simple-cache (1.0.1): Extracting archive
remote:          - Installing opis/closure (3.6.2): Extracting archive
remote:          - Installing symfony/translation-contracts (v3.0.0): Extracting archive
remote:          - Installing symfony/translation (v6.0.1): Extracting archive
remote:          - Installing nesbot/carbon (2.55.2): Extracting archive
remote:          - Installing monolog/monolog (2.3.5): Extracting archive
remote:          - Installing league/mime-type-detection (1.9.0): Extracting archive
remote:          - Installing league/flysystem (1.1.9): Extracting archive
remote:          - Installing nette/utils (v3.2.6): Extracting archive
remote:          - Installing nette/schema (v1.2.2): Extracting archive
remote:          - Installing dflydev/dot-access-data (v3.0.1): Extracting archive
remote:          - Installing league/config (v1.1.1): Extracting archive
remote:          - Installing league/commonmark (2.1.0): Extracting archive
remote:          - Installing laravel/serializable-closure (v1.0.5): Extracting archive
remote:          - Installing laravel/framework (v8.77.1): Extracting archive
remote:          - Installing asm89/stack-cors (v2.0.3): Extracting archive
remote:          - Installing fruitcake/laravel-cors (v2.0.4): Extracting archive
remote:          - Installing psr/http-message (1.0.1): Extracting archive
remote:          - Installing psr/http-client (1.0.1): Extracting archive
remote:          - Installing ralouphie/getallheaders (3.0.3): Extracting archive
remote:          - Installing guzzlehttp/psr7 (2.1.0): Extracting archive
remote:          - Installing guzzlehttp/promises (1.5.1): Extracting archive
remote:          - Installing guzzlehttp/guzzle (7.4.1): Extracting archive
remote:          - Installing laravel/sanctum (v2.13.0): Extracting archive
remote:          - Installing nikic/php-parser (v4.13.2): Extracting archive
remote:          - Installing psy/psysh (v0.10.12): Extracting archive
remote:          - Installing laravel/tinker (v2.6.3): Extracting archive
remote:          - Installing laravel-lang/lang (5.0.0): Extracting archive
remote:          - Installing overtrue/laravel-lang (4.2.2): Extracting archive
remote:        Package swiftmailer/swiftmailer is abandoned, you should avoid using it. Use symfony/mailer instead.
remote:        Generating optimized autoload files
remote:        > Illuminate\Foundation\ComposerScripts::postAutoloadDump
remote:        > @php artisan package:discover --ansi
remote:        Discovered Package: fruitcake/laravel-cors
remote:        Discovered Package: laravel/sanctum
remote:        Discovered Package: laravel/tinker
remote:        Discovered Package: nesbot/carbon
remote:        Discovered Package: overtrue/laravel-lang
remote:        Package manifest generated successfully.
remote:        50 packages you are using are looking for funding.
remote:        Use the `composer fund` command to find out more!
remote: -----> Preparing runtime environment...
remote: -----> Checking for additional extensions to install...
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 23.2M
remote: -----> Launching...
remote:        Released v20
remote:        https://app-wangqiang-weibo.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/app-wangqiang-weibo.git
   626a3e3..f5fb1ef  main -> main
PS E:\wwwroot\weibo> heroku run php artisan migrate:refresh --seed
 »   Warning: heroku update available from 7.53.0 to 7.59.2.
Running php artisan migrate:refresh --seed on ⬢ app-wangqiang-weibo... up, run.2437 (Free)
**************************************
*     Application In Production!     *
**************************************

 Do you really wish to run this command? (yes/no) [no]:
 > yes

Rolling back: 2021_12_24_062642_add_is_admin_to_users_table
Rolled back:  2021_12_24_062642_add_is_admin_to_users_table (10.82ms)
Rolling back: 2019_12_14_000001_create_personal_access_tokens_table
Rolled back:  2019_12_14_000001_create_personal_access_tokens_table (15.24ms)
Rolling back: 2019_08_19_000000_create_failed_jobs_table
Rolled back:  2019_08_19_000000_create_failed_jobs_table (11.88ms)
Rolling back: 2014_10_12_100000_create_password_resets_table
Rolled back:  2014_10_12_100000_create_password_resets_table (10.93ms)
Rolling back: 2014_10_12_000000_create_users_table
Rolled back:  2014_10_12_000000_create_users_table (12.13ms)
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (32.83ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (16.75ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (23.02ms)
Migrating: 2019_12_14_000001_create_personal_access_tokens_table
Migrated:  2019_12_14_000001_create_personal_access_tokens_table (24.03ms)
Migrating: 2021_12_24_062642_add_is_admin_to_users_table
Migrated:  2021_12_24_062642_add_is_admin_to_users_table (6.28ms)
Seeding: Database\Seeders\UsersTableSeeder
Seeded:  Database\Seeders\UsersTableSeeder (827.06ms)
Database seeding completed successfully.
PS E:\wwwroot\weibo>

 

永夜