Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

uhexos's avatar

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

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;
        });
    }
}
0 likes
5 replies
tykus's avatar

Something does not add up here... you alias the Rap2hpoutre\FastExcel\FastExcel class, but the error message references the Rap2hpoutre\FastExcel\Facades\FastExcel Facade class???

Edit Did you also register the Facade - it mentions that you will not have access to the constructor

tykus's avatar

@uhexos possibly registering the Facade and then trying to use the constructor as well??? Use one approach or the other.

uhexos's avatar

not sure I follow I have no facade registered '''

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

''' just installed the library and used the example from their docs

uhexos's avatar

Seems the issue was a caching issue, I had most likely imported wrongly and the started the php artisan queue:work restarting this fixed the issue

Please or to participate in this conversation.