How to access a class that is inside a folder in the Laravel vendor?

Asked

Viewed 332 times

0

I can’t access Spreadsheet class, it appears:

use Illuminate\Database\Eloquent\Model;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class Excel extends Model
{
    public function excelPropostas($ini = null, $fim = null, $qtd = null)
    {
        $spreadsheet = new Spreadsheet();
        ...
    }
}

Class of the Spreaadsheet:

namespace PhpOffice\PhpSpreadsheet;

class Spreadsheet
{
..
}

but makes a mistake:

Class 'PhpOffice\PhpSpreadsheet\Spreadsheet' not found

Can someone help me?

  • If you installed it by Composer, all you had to do was use the namespace PhpOffice\PhpSpreadsheet\Spreadsheet, without this Vendor at first, something else does not move its dependency out of the vendor folder of the project, and the namespace of a file does not depend on the location of the file in the directory, but on which namespace is used in the file that you import, by convention the same namespace is used for the directory hierarchy. I recommend doing a better search for namespace in php. https://answall.com/questions/151487/namespaces-e-use-quando-usar-e-para-que-servem.

  • I edited the question, in fact it the directory, but still, it does not find the class. Unless the problem is to load it in Laravel’s own Model format.

  • You installed him by the Commodore?

  • Yes, I installed it by Composer.

  • I recommend removing the vendor folder from the project and installing again, and then generating autoload again, as follows: Remove vendor folder rm -rf vendor then install the dependencies composer install and lastly composer dump-autoload. And I give the tip never manually change any file inside the vendor directory, it will only bring future problems.

  • The error is in the class call: $Spreadsheet = new Spreadsheet(); This folder is not native to the Laravel, so there is no point in reinstalling it. But I’ll try to dump-autoload.

  • Didn’t work..

Show 2 more comments

1 answer

0


I couldn’t find a way to resolve this manually, other than by manipulating a file in a directory outside of Alavel. However I found this service on the web, already tested and works: https://laravel-excel.com/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.