Custom import job

Getting a serializable error when trying to import using a custom job: Cannot initialize readonly property Filament\Actions\Imports\Jobs\ImportCsv::$importer from scope App\Jobs\ImportPatients (attached error message) I am tryin to import patients, patients is a model that needs Users model to exists, where patient belongs to user I have managed to create an import action, but that does not create the user with the patient, so I am resorting to creating an importer with a custom job $table->headerActions([ ImportAction::make() ->importer(PatientImporter::class) ->job(ImportPatients::class) ]) I can't figure out what's wrong, and there is no duocumentation on this.
Solution:
if you want to create the relationship(user) along with the patient record, make use of the beforeSave hook ```protected function beforeSave(): void { // }...
Jump to solution
13 Replies
bakriawad
bakriawad7mo ago
this is what the job looks like: <?php namespace App\Jobs; use App\Filament\Common\PatientUpsertHelper; use Filament\Actions\Imports\Jobs\ImportCsv; use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Support\Facades\DB; use Illuminate\Validation\ValidationException; use Throwable; class ImportPatients extends ImportCsv { public function handle(): void { /** @var Authenticatable $user */ $user = $this->import->user; auth()->login($user); $exceptions = []; foreach ($this->rows as $row) { try { dd($row); DB::transaction(function () use ($row, $user) { $patient = PatientUpsertHelper::create($row, $user); return ($this->importer)($patient); }); } catch (ValidationException $exception) { $this->logFailedRow($row, collect($exception->errors())->flatten()->implode(' ')); } catch (Throwable $exception) { $exceptions[$exception::class] = $exception; $this->logFailedRow($row); } $this->import->increment('processed_rows'); } $this->handleExceptions($exceptions); } } The PatientUpsertHelper::create function is also being used by the dashboard to insert patients one at a time, and that works fine, it returns the patient data that is to be saved including the foreign key
Hussain4real
Hussain4real7mo ago
custom import Job is not necessary, the ImporterClass takes care of creating belongsTo relationship
bakriawad
bakriawad7mo ago
it doesn't seem to make them #Importing relationships .... In this example, the author column in the CSV will be mapped to the author_id column in the database. The CSV should contain the primary keys of authors, usually id. If the column has a value, but the author cannot be found, the import will fail validation. Filament automatically adds validation to all relationship columns, to ensure that the relationship is not empty when it is required. from the docs, the second paragarph clearly says it will fail if relationship is not there
Solution
Hussain4real
Hussain4real7mo ago
if you want to create the relationship(user) along with the patient record, make use of the beforeSave hook
protected function beforeSave(): void
{
//
}
protected function beforeSave(): void
{
//
}
Hussain4real
Hussain4real7mo ago
but to achieve this your csv must have colums that contains data for creating the user
bakriawad
bakriawad7mo ago
there is data to make the user, first name and last name for example are user data, not patient also beforeSave might not work becuase of the mapping? I will try
Hussain4real
Hussain4real7mo ago
it will work just mapp the user data to the user_id column i implemented similar thing that is more complicated than your scenario let me know hoe it goes
bakriawad
bakriawad7mo ago
That actually worked... my god I can't believe I didn't think of this. I didn't have to change mappings or anything, just set the models correctly. Thank you! not sure how to make this resolved
Hussain4real
Hussain4real7mo ago
nice to hear it worked you can use the apps feature to mark resolve
bakriawad
bakriawad7mo ago
where is that?
bakriawad
bakriawad7mo ago
found it. Thank you very much!
Hussain4real
Hussain4real7mo ago
my pleasure