rap2hpoutre / fast-excel

🦉 Fast Excel import/export for Laravel
MIT License
2.08k stars 246 forks source link

Call to undefined method Rap2hpoutre\FastExcel\Facades\FastExcel::import() in laravel 10 job although the method exists #330

Closed uhexos closed 12 months ago

uhexos commented 1 year ago

Have a controller given below when the (new FastExcel)->import($fullPath, function ($row) portion is uncommented, I can see users being inserted into the database as expeected. However when I move this logic to a job I get

Call to undefined method Rap2hpoutre\FastExcel\Facades\FastExcel::import()

the method clearly exists as I am able to use it in the controller and only fails in the job. Any ideas why this happens. using laravel 10 and "rap2hpoutre/fast-excel": "^5.2"

<?php

namespace App\Http\Controllers;

use App\Http\Requests\UploadContactsRequest;
use App\Imports\ContactsImport;
use App\Jobs\ProcessContactCsv;
use App\Models\Contact;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;
use Rap2hpoutre\FastExcel\FastExcel;

class ContactController extends Controller
{
    public function import(UploadContactsRequest $uploadContactsRequest)
    {
        $file = $uploadContactsRequest->file('file');

        $path = $file->store('temp');
        $fullPath = Storage::path($path);

        ProcessContactCsv::dispatch($fullPath, auth()->user()->id);

        // $contacts = (new FastExcel)->import($fullPath, function ($row) {
        //     $contact = new Contact([
        //         'firstname' => $row['firstname'],
        //         'lastname' => $row['lastname'],
        //         'date_of_birth' => $row['date_of_birth']

        //     ]);
        //     $contact->owner_id = auth()->user()->id;
        //     $contact->save();
        //     return $contact;
        // });
        return redirect('/')->with('success', 'All good!');
    }
}

<?php

namespace App\Jobs;

use App\Models\Contact;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Rap2hpoutre\FastExcel\FastExcel;

class ProcessContactCsv implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $path;
    protected $ownerId;

    /**
     * Create a new job instance.
     */
    public function __construct(string $path, string $ownerId,)
    {
        $this->path = $path;
        $this->ownerId = $ownerId;
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        $contacts = (new FastExcel)->import($this->path, function ($row) {
            $contact = new Contact([
                'firstname' => $row['firstname'],
                'lastname' => $row['lastname'],
                'date_of_birth' => $row['date_of_birth']
            ]);
            $contact->owner_id = auth()->user()->id;
            $contact->save();
            return $contact;
        });
    }
}
StefanoTesla commented 1 year ago

does dump auto-load return some errors?

rap2hpoutre commented 12 months ago

I just released a new version, could you check again and reopen this ticket if the problem still occurs?