F
Filament2y ago
Anik

multiple models with relationships with Wizard Steps

Hi, I am using a Wizard Step with a model on a custom page to create and edit the model. I am having issues with SpatieMediaUpload which doesn't save the relation the first time on create but works well on edit. Sample code below
in Mount
$this->kyc = is_null($existKycRecord) ? $this->user->kyc()->getModel() : $existKycRecord;

Step::make('kyc')->model($this->kyc)
->afterValidation(function (Closure $get) {
$kycForm = [
'type' => $get('user_type'),
'has_tax' => $get('has_tax'),
];
$this->saveKyc($kycForm);
}),

Save function
public function saveKyc($data)
{
$this->kyc = $this->kyc->updateOrCreate(['user_id' => $this->user->id], $data);

$this->form->model($this->kyc)->saveRelationships();

$this->notify('success', 'User KYC Saved successfully!');

}
in Mount
$this->kyc = is_null($existKycRecord) ? $this->user->kyc()->getModel() : $existKycRecord;

Step::make('kyc')->model($this->kyc)
->afterValidation(function (Closure $get) {
$kycForm = [
'type' => $get('user_type'),
'has_tax' => $get('has_tax'),
];
$this->saveKyc($kycForm);
}),

Save function
public function saveKyc($data)
{
$this->kyc = $this->kyc->updateOrCreate(['user_id' => $this->user->id], $data);

$this->form->model($this->kyc)->saveRelationships();

$this->notify('success', 'User KYC Saved successfully!');

}
The form isn't saved when saveKyc is run the first time (on wizard next) but the function saves the relationships if it's run a second time (going previous and next on the wizard). I am looking for a way to save a multi model form with attachments via SpatieMedia.
1 Reply
Anik
AnikOP2y ago
this works correctly if we use multiple forms instead of this->form, then can we cannot use the wizard component. Most probably an issue on this line on create. ` $this->form->model($this->kyc)->saveRelationships();

Did you find this page helpful?