F
Filamentvanhellsing101

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 😝...
Solution
V
vanhellsing101β€’12d 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 😝
B
Bonuxβ€’8d ago
@vanhellsing101 can you show me an example of how you did this?
V
vanhellsing101β€’7d 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
More Posts
Is it possible to implement HasOne relationship in Registration page?I am trying to register the user with overriding default Register pages to have Wizard and Fieldset.Multiple Field in Custom FieldHi, pls help. How can I handle or get the 2 values from a custom field?How to make table groups have a specific order?For example, I'm grouping by status but i would like the groups to be listed in a specific order. NoAdd Alpine Collapse plugin to dashboardI've tried a few methods of adding the script tag for enabling the collapse alpine plugin (https://aGroup resource list without model IDI'm trying to create a page that will bring the records of Student Certificates from the group groupOnly one collapsible element expanded at a timeI'm using the section blade component. When a user clicks on one element, I want it to expand, and cUnhandled match case '...'Its caused by ``` <x-filament::icon-button icon="heroicon-o-user-circle" size="w-12 h-12" label="NeBranding a panel with tenant infoI was hoping to use a tenants name, logo, color, etc for the branding of a panel but the tenant hasnEnable Echo.js without breaking Hot ReloadOk I'm at my wit's end here... I know this is probably not strictly Filament related, but maybe therTextEntry when value is null```php TextEntry::make('strap') ->columnSpanFullfilament tabs from blade dont have contentHow can I add content to the tabs directly from blade? As it is now, Its just the tab items themselvHow do I make the bar chart widget to take the full height here?