How to import belongsToMany fields in the filament importer?

public static function getColumns(): array
{
return [
ImportColumn::make('first_name')
->requiredMapping()
->rules(['required', 'max:255'])
->example('John'),
ImportColumn::make('last_name')
->requiredMapping()
->rules(['required', 'max:255'])
->example('Doe'),
ImportColumn::make('email')
->requiredMapping()
->rules(['required', 'email', 'max:255'])
->example('[email protected]'),
// ImportColumn::make('company_name')
// ->relationship('companies', 'name')
// ->rules(['required', 'max:255', 'exists:mvl_companies,name'])
// ->example('Best Cycles Inc.'),
// ImportColumn::make('company_employee_id')
// ->rules(['required', 'max:255'])
// ->example('BCI-123'),
// ImportColumn::make('company_employee_budget')
// ->numeric()
// ->rules(['required'])
// ->example('10.00,23')
];
}
public static function getColumns(): array
{
return [
ImportColumn::make('first_name')
->requiredMapping()
->rules(['required', 'max:255'])
->example('John'),
ImportColumn::make('last_name')
->requiredMapping()
->rules(['required', 'max:255'])
->example('Doe'),
ImportColumn::make('email')
->requiredMapping()
->rules(['required', 'email', 'max:255'])
->example('[email protected]'),
// ImportColumn::make('company_name')
// ->relationship('companies', 'name')
// ->rules(['required', 'max:255', 'exists:mvl_companies,name'])
// ->example('Best Cycles Inc.'),
// ImportColumn::make('company_employee_id')
// ->rules(['required', 'max:255'])
// ->example('BCI-123'),
// ImportColumn::make('company_employee_budget')
// ->numeric()
// ->rules(['required'])
// ->example('10.00,23')
];
}
public function resolveRecord(): ?User
{
return User::firstOrNew([
'email' => $this->data['email'],
]);
}
public function resolveRecord(): ?User
{
return User::firstOrNew([
'email' => $this->data['email'],
]);
}
The getColumns() has the 3 users table fields on the top followed by the related user_company (pivot table) fields i.e. company_employee_id and company_employee_budget allong with the company name from the companies table commented out. How to I ensure that I am able to add a user and then find the company by name and attach that company with the newly inserted or updated user?
5 Replies
jagato0
jagato011mo ago
Hi! I think is easier to use hooks and overwrite the saveRecord function in order to handle everything you need or as a last resource. Good luck!
headbanger
headbangerOP10mo ago
@jagato0 can you elaborate more on the approach please?
_andypeacock
_andypeacock10mo ago
@headbanger did you find an answer? I'm looking for a way to do the same, but can't get the before save to even respond to a dd() to debug it
headbanger
headbangerOP10mo ago
@_andypeacock I had to resort to using an external package to make this work
Hussain4real
Hussain4real10mo ago
you can use the beforeSave hook
Want results from more Discord servers?
Add your server