ImportAction with relationship not resolving

I get validation error "The selected classifiedCategory is invalid." in my failed queue. I have ClassifiedCategory and ClassifiedSubcategory models with 1:N relationship. I have classifiedCategory() relationship method in ClassifiedSubcategory model. Instead of using ID in CSV I am trying to resolve using slug. Below is the code.

ImportColumn::make('classifiedCategory')
->relationship(resolveUsing: function (array $state): ?ClassifiedCategory{
return ClassifiedCategory::query()
->where('slug', $state)
->first();
})
->requiredMapping()
->exampleHeader('ClassifiedCategory Slug'),

ImportColumn::make('classifiedCategory')
->relationship(resolveUsing: function (array $state): ?ClassifiedCategory{
return ClassifiedCategory::query()
->where('slug', $state)
->first();
})
->requiredMapping()
->exampleHeader('ClassifiedCategory Slug'),

Resolving using ID in CSV with below code works though
ImportColumn::make('classifiedCategory')
->relationship()
->requiredMapping()
->exampleHeader('ClassifiedCategory ID'),
ImportColumn::make('classifiedCategory')
->relationship()
->requiredMapping()
->exampleHeader('ClassifiedCategory ID'),
1 Reply
Pritbor
PritborOP9mo ago
BTW I did try originally this code but got same error.
ImportColumn::make('classifiedCategory')
->relationship(resolveUsing: 'slug')
->requiredMapping()
->exampleHeader('ClassifiedCategory Slug')
ImportColumn::make('classifiedCategory')
->relationship(resolveUsing: 'slug')
->requiredMapping()
->exampleHeader('ClassifiedCategory Slug')
Want results from more Discord servers?
Add your server