1

I'm creating my own Laravel package for the first time. I create a new project and require orchestra/testbench in the project. Things look okay and I'm able to run tests inside the package but I couldn't use Laravel Excel inside my package.

in composer.json I added

"extra": {
    "laravel": {
        "providers": [
            "Maatwebsite\\Excel\\ExcelServiceProvider"
        ],
        "aliases": {
            "Excel": "Maatwebsite\\Excel\\Facades\\Excel",
        }
    }
},
"require-dev": {
    "orchestra/testbench": "6.0",
    "phpunit/phpunit": "^9.5",
    "maatwebsite/excel": "^3.1"
}

And also ran composer dump-autoload, when I want to use Laravel Excel inside my package I tried

use Maatwebsite\Excel\Facades\Excel;

class TaxCalculation {

    public function incomeTax(): float
    {
        $table = Excel::import(new TaxImport(), 'file.xls');
    }

But got an error

Illuminate\Contracts\Container\BindingResolutionException: Target class [excel] does not exist.

D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Container\Container.php:832
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Container\Container.php:712
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:796
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Container\Container.php:651
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:781
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Container\Container.php:1354
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:198
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:166
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:255
D:\code\packages\thai_tax\src\Services\TaxCalculation.php:45
D:\code\packages\thai_tax\tests\Unit\CreateFacadeTest.php:26

Caused by
ReflectionException: Class excel does not exist

Excel does not seem to load. Is there any more steps I need to do to use it?

Anurat Chapanond
  • 2,837
  • 3
  • 18
  • 31
  • this bug still happening, somehow the service provider does not get registered during tests... you can manually add ``Maatwebsite\Excel\ExcelServiceProvider::class``, in your ``config/app.php`` to solve this issue – Sok Chanty Jan 05 '21 at 06:25
  • Thanks @sok-chanty but I do not have `config/app.php` since I create a new project outside Laravel project and use `orchestra/testbench` instead. – Anurat Chapanond Jan 05 '21 at 06:29
  • Anyway, why you include laravel? You might include the correct tag that you will got the answer. – Sok Chanty Jan 05 '21 at 06:31

0 Answers0