Import Action: Import data for the belongsTo relationship without having foreign_id in CSV itself?

I need to import a CSV for the resource that belongsTo to another resource eg: I have a company and I need to import the staff for that company. But reading the document for the import, the only way we can do that is by having the company_id in the CSV itself. Is there a way to pass the company_id by ourselves in the Importer?
Solution:
I have solved it using the options method that ImportAction provides (it was staring right at me in the docs πŸ˜…) https://filamentphp.com/docs/3.x/actions/prebuilt-actions/import#using-import-options First I used the mutateFormDataUsing ofActions and passed id there and fetched that using the getOptions of the importer, which worked but I was still not satisfied and remembered that there are the options in the docs too and I was doing something extra. I just opened the doc tab I had already opened and there it was. Well it is always good to try many possibilities 😝...
Jump to solution
3 Replies
Solution
vanhellsing101
vanhellsing101β€’8mo ago
I have solved it using the options method that ImportAction provides (it was staring right at me in the docs πŸ˜…) https://filamentphp.com/docs/3.x/actions/prebuilt-actions/import#using-import-options First I used the mutateFormDataUsing ofActions and passed id there and fetched that using the getOptions of the importer, which worked but I was still not satisfied and remembered that there are the options in the docs too and I was doing something extra. I just opened the doc tab I had already opened and there it was. Well it is always good to try many possibilities 😝
Bonux
Bonuxβ€’8mo ago
@vanhellsing101 can you show me an example of how you did this?
vanhellsing101
vanhellsing101OPβ€’8mo ago
Sure, ImportAction
ImportAction::make()
->importer(StaffImporter::class)
->options([
'client_id' => //data you need pass,
]),
ImportAction::make()
->importer(StaffImporter::class)
->options([
'client_id' => //data you need pass,
]),
In the Importer you can now access the client_id using $this->options['client_id'] example in resolveRecord method
public function resolveRecord(): ?Staff
{
$client_id = $this->options['client_id'];

return Staff::firstOrNew([
'client_id' => $client_id,
'email' => $this->data['email'],
]);
}
public function resolveRecord(): ?Staff
{
$client_id = $this->options['client_id'];

return Staff::firstOrNew([
'client_id' => $client_id,
'email' => $this->data['email'],
]);
}
Want results from more Discord servers?
Add your server