You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel安装maatwebsite/excel后执行artisan命令报share方法不存在错误

Laravel中Maatwebsite/Excel执行make:export命令报错的解决办法

问题详情

在Laravel项目中通过composer require maatwebsite/excel安装包后,已手动配置好服务提供者和别名,但执行php artisan make:export ExportTimesheet --model=Timesheet时抛出错误:

BadMethodCallException Method Illuminate\Foundation\Application::share does not exist

报错代码片段:

protected function bindClasses()
{
    // Bind the format identifier
    $this->app['excel.identifier'] = $this->app->share(function($app) {
        return new FormatIdentifier($app['files']);
    });
}

当前配置信息

config/app.php中的服务提供者与别名

'providers' => [

    /*
     * Laravel Framework Service Providers...
     */
    Illuminate\Auth\AuthServiceProvider::class,
    Illuminate\Broadcasting\BroadcastServiceProvider::class,
    Illuminate\Bus\BusServiceProvider::class,
    Illuminate\Cache\CacheServiceProvider::class,
    Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
    Illuminate\Cookie\CookieServiceProvider::class,
    Illuminate\Database\DatabaseServiceProvider::class,
    Illuminate\Encryption\EncryptionServiceProvider::class,
    Illuminate\Filesystem\FilesystemServiceProvider::class,
    Illuminate\Foundation\Providers\FoundationServiceProvider::class,
    Illuminate\Hashing\HashServiceProvider::class,
    Illuminate\Mail\MailServiceProvider::class,
    Illuminate\Notifications\NotificationServiceProvider::class,
    Illuminate\Pagination\PaginationServiceProvider::class,
    Illuminate\Pipeline\PipelineServiceProvider::class,
    Illuminate\Queue\QueueServiceProvider::class,
    Illuminate\Redis\RedisServiceProvider::class,
    Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
    Illuminate\Session\SessionServiceProvider::class,
    Illuminate\Translation\TranslationServiceProvider::class,
    Illuminate\Validation\ValidationServiceProvider::class,
    Illuminate\View\ViewServiceProvider::class,
    Maatwebsite\Excel\ExcelServiceProvider::class,

    /*
     * Package Service Providers...
     */

    /*
     * Application Service Providers...
     */
    App\Providers\AppServiceProvider::class,
    App\Providers\AuthServiceProvider::class,
    // App\Providers\BroadcastServiceProvider::class,
    App\Providers\EventServiceProvider::class,
    App\Providers\RouteServiceProvider::class,

],

/*
 |--------------------------------------------------------------------------
 | Class Aliases
 |--------------------------------------------------------------------------
 |
 | This array of class aliases will be registered when this application
 | is started. However, feel free to register as many as you wish as
 | the aliases are "lazy" loaded so they don't hinder performance.
 |
 */

'aliases' => Facade::defaultAliases()->merge([
    // 'ExampleClass' => App\Example\ExampleClass::class,
    'Excel' => Maatwebsite\Excel\Facades\Excel::class,
])->toArray(),

composer.json配置

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "require": {
        "php": "^8.0.2",
        "guzzlehttp/guzzle": "^7.2",
        "laravel/framework": "^9.19",
        "laravel/sanctum": "^3.0",
        "laravel/tinker": "^2.7",
        "maatwebsite/excel": "*",
        "spatie/laravel-permission": "^5.8"
    },
    "require-dev": {
        "fakerphp/faker": "^1.9.1",
        "laravel/pint": "^1.0",
        "laravel/sail": "^1.0.1",
        "mockery/mockery": "^1.4.4",
        "nunomaduro/collision": "^6.1",
        "phpunit/phpunit": "^9.5.10",
        "spatie/laravel-ignition": "^1.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-update-cmd": [
            "@php artisan vendor:publish --tag=laravel-assets --ansi --force"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true,
        "allow-plugins": {
            "pestphp/pest-plugin": true
        }
    },
    "minimum-stability": "stable",
    "prefer-stable": true
}

问题原因

share()方法在Laravel 5.4版本后已被移除,替代方案为singleton()方法或单例绑定模式。你当前安装的maatwebsite/excel版本过低,不兼容Laravel 9(你的项目使用Laravel ^9.19)。由于composer.json中设置了maatwebsite/excel": "*",Composer可能自动安装了2.1及以下的旧版本,这些版本依赖已废弃的share()方法,导致报错。

解决步骤

  1. 安装兼容Laravel 9的版本
    执行命令安装3.1及以上版本的maatwebsite/excel:

    composer require maatwebsite/excel "^3.1"
    
  2. 移除手动配置(可选)
    Laravel 5.5及以上版本支持包自动发现,可删除config/app.php中手动添加的Maatwebsite\Excel\ExcelServiceProvider::class和'Excel' => Maatwebsite\Excel\Facades\Excel::class,Composer会自动处理配置。

  3. 清除缓存
    执行以下命令清理配置和应用缓存:

    php artisan config:clear
    php artisan cache:clear
    
  4. 重新执行生成命令
    再次运行导出类生成命令:

    php artisan make:export ExportTimesheet --model=Timesheet
    

内容的提问来源于stack exchange,提问作者Leonardo Gori

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 00:01:20