zaidpirwani
Import action multiple models
I got it to work, - creating and saving other records / child records when importing
I have additional columns in my csv, but only the required/main model columns in the getColumns method - I have also added a couple of OPTIONS on the upload modal itself
the options are used to add additional info to the main record for the importer
I use afterFill for that
and then I am using afterSave to create child records from other models and records and get data of the additional columns from $this->originalData object
below is some code - I have removed unnecessary stuff - so may not be directly usable, but it conveys the message - maybe error and I have not worked at UPDATING the original model or its child records - so that is an exercise for later
class QuestionImporter extends Importer
{
protected static ?string $model = Question::class;
public static function getColumns(): array
{
return [
ImportColumn::make('text')
->requiredMapping()
->rules(['required'])
->guess(['Question-Text']),
ImportColumn::make('neverShuffleOptions')
->requiredMapping()
->boolean()
->rules(['required', 'boolean'])
->guess(['Shuffle-Options']),
];
}
public static function getOptionsFormComponents(): array
{
return [
Select::make('quiz_id')
->relationship('quiz', 'name')
->label('Quiz'),
];
}
protected function afterFill(): void
{
$this->record->user_id = auth()->user()->id;
$this->record->quiz_id = $this->options['quiz_id'];
}
protected function afterSave(): void
{
$newAnswer = Answer::create([
'text' => $this->originalData['Answer-Text'],
'image' => $this->originalData['Answer-Image'],
'question_id' => $this->record->id,
]);
}
11 replies
Import action multiple models
I am just starting on this, so as per docs
I will try with
protected function afterSave(): void
{
// Runs after a record is saved to the database.
}
and then use $this->data, $this->record and $this->originalData
https://filamentphp.com/docs/3.x/actions/prebuilt-actions/import#lifecycle-hooks
11 replies
Import action multiple models
did you find a solution to this ?
Currently I have 2 imports, one for my questions model and another for my answers model
I would LOVE to be able to import questions and answers all from one CSV
questionText, questionImage, answerAText, answerAImage,answerACorrect, answerBText, answerBImage,answerBCorrect, answerCText, answerCImage,answerCCorrect, answerDText, answerDImage,answerDCorrect
I would like the importer to create a question record first and then create 4 answer records that link to the new question record
so users can create only 1 excel sheet, one simple CSV and no need to worry about resolving question or knowing question ID for answer import
11 replies
Get info of the selected record in AttachAction
for now, I had to add an additional TextInput field and am updating its value from the afterStateUpdated method of action
but I would like for this to be handled via the suffix and use closure and get to get the current value of the recordselect, but what is its path ?
23 replies
Get info of the selected record in AttachAction
@barrerakj did you resolve this issue - I kind of stumbled on the same problem. how do I get the recordselect option to use in other fields of the AttachAction form inside a relationship ?
->headerActions([
Tables\Actions\AttachAction::make()
->preloadRecordSelect()
->form(fn(AttachAction $action): array => [
$action
->getRecordSelect()
->placeholder('Select an item')
->live(onBlur: true)
->required(),
// ->afterStateUpdated(fn(Set $set, $state) => $set('unitOfMeasure',
// optional(Item::find($state))->unitOfMeasure->name)),
Forms\Components\TextArea::make('description')
->live()
->required(),
Forms\Components\TextInput::make('quantity')
->required()
->inputMode('numeric')
->minValue(1)
->default(1)
->live(onBlur: true)
->debounce(1000)
->suffix(fn(Get $get) => $get('recordSelect ????'))
->columnSpan(1),
// Forms\Components\TextInput::make('unitOfMeasure')
// ->disabled()
// ->label('Unit')
// ->columnSpan(1),
Forms\Components\TextInput::make('rate')
->required()
->inputMode('numeric')
->minValue(0.1)
->default(1)
->debounce(1000)
->live()
->columnSpan(1),
Forms\Components\Placeholder::make('amount')
->content(fn(Get $get) => $get('quantity') * $get('rate'))
->disabled(),
])
->after(function (Component $livewire) {
$livewire->dispatch('refreshProcurementRequest');
}),
])
23 replies