ImportAction filling relational data from multiple columns

HI, I am making an importer for standard company table of my projects. Each company has an invoice and regular address. I saw the resolveUsing but this only allows you to resolve using the state of one field. For address relation I need to check for multiple fields. I tried resolving the following:
public function resolveRecord(): ?Model
{
try {

$address = Address::firstOrCreate([
'street' => $this->data['street'],
'number' => $this->data['number'],
'postcode' => $this->data['postcode'],
'city' => $this->data['city'],
'country' => $this->data['country'],
]);

$company = Company::updateOrCreate([
'company_code' => $this->data['code'],
], [
'name' => $this->data['name'],
'address_id' => $address->id,
'phone_number' => $this->data['phone'],
'email' => $this->data['email'],
'kvk_number' => $this->data['kvk'],
'btw_number' => $this->data['vat'],
]);

return $company;
} catch (\Exception $e) {
throw new RowImportFailedException("not created: " . $e->getMessage());
}
}
public function resolveRecord(): ?Model
{
try {

$address = Address::firstOrCreate([
'street' => $this->data['street'],
'number' => $this->data['number'],
'postcode' => $this->data['postcode'],
'city' => $this->data['city'],
'country' => $this->data['country'],
]);

$company = Company::updateOrCreate([
'company_code' => $this->data['code'],
], [
'name' => $this->data['name'],
'address_id' => $address->id,
'phone_number' => $this->data['phone'],
'email' => $this->data['email'],
'kvk_number' => $this->data['kvk'],
'btw_number' => $this->data['vat'],
]);

return $company;
} catch (\Exception $e) {
throw new RowImportFailedException("not created: " . $e->getMessage());
}
}
However this still tries to insert all the ImportColumns into company, even street, number etc... while these fields are not in my company model. Am I missing something here? I thought reading the docs very carefully that this was the way to go. I inserted a screenshot with my import columns. In my log file it tries to insert all the data found and returns the following update:
update `companies` set `company_code` = NL, `street` = XXX, `number` = XXX, `postcode` = XXX, `city` = XXX, `country` = NL, `phone` = ?, `kvk` = ?, `vat` = ?, `companies`.`updated_at` = 2024-12-10 12:43:19 where `id` = 12
update `companies` set `company_code` = NL, `street` = XXX, `number` = XXX, `postcode` = XXX, `city` = XXX, `country` = NL, `phone` = ?, `kvk` = ?, `vat` = ?, `companies`.`updated_at` = 2024-12-10 12:43:19 where `id` = 12
Clearly mixing the address table and company table. Does anyone have something like this working? Thanks in advance!
No description
2 Replies
gladjanus43
gladjanus43OP2w ago
it is failing in the queue. But it is not giving any Validation Errors in the failed_import_rows
No description
toeknee
toeknee2w ago
I would do:
public function resolveRecord(): ?Model
{
try {

$address = Address::firstOrCreate([
'street' => trim(data_get($this->data, 'street', ''))),
'number' => trim(data_get($this->data, 'number', ''))),
'postcode' => trim(data_get($this->data, 'postcode', ''))),
'city' => trim(data_get($this->data, 'city', ''))),
'country' => trim(data_get($this->data, 'country', ''))),
]);

if (!$address) {
throw new RowImportFailedException('Failed to create or find address');
}

$company = Company::updateOrCreate([
'company_code' => trim(data_get($this->data, 'code', ''))),
], [
'name' => trim(data_get($this->data, 'name', ''))),
'address_id' => $address->id,
'phone_number' => trim(data_get($this->data, 'phone', ''))),
'email' => trim(data_get($this->data, 'email', ''))),
'kvk_number' => trim(data_get($this->data, 'kvk', ''))),
'btw_number' => trim(data_get($this->data, 'vat', ''))),
]);

return $company;
} catch (\Exception $e) {
throw new RowImportFailedException("not created: " . $e->getMessage());
}
}
public function resolveRecord(): ?Model
{
try {

$address = Address::firstOrCreate([
'street' => trim(data_get($this->data, 'street', ''))),
'number' => trim(data_get($this->data, 'number', ''))),
'postcode' => trim(data_get($this->data, 'postcode', ''))),
'city' => trim(data_get($this->data, 'city', ''))),
'country' => trim(data_get($this->data, 'country', ''))),
]);

if (!$address) {
throw new RowImportFailedException('Failed to create or find address');
}

$company = Company::updateOrCreate([
'company_code' => trim(data_get($this->data, 'code', ''))),
], [
'name' => trim(data_get($this->data, 'name', ''))),
'address_id' => $address->id,
'phone_number' => trim(data_get($this->data, 'phone', ''))),
'email' => trim(data_get($this->data, 'email', ''))),
'kvk_number' => trim(data_get($this->data, 'kvk', ''))),
'btw_number' => trim(data_get($this->data, 'vat', ''))),
]);

return $company;
} catch (\Exception $e) {
throw new RowImportFailedException("not created: " . $e->getMessage());
}
}
Want results from more Discord servers?
Add your server