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
<?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;
});
}
}
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