Forms\Components\FileUpload::make('images')
->multiple() // Allow multiple file uploads
->label('Product Images')
->after(function ($state, $model) {
// Save the images after the product is saved
if ($state) {
foreach ($state as $image) {
$model->images()->create([
'path' => $image, // Path to the uploaded image
]);
}
}
}),
@tisuchi Hi unfortunatelly it is not the solution. I have the relationship on the model but it doesn't seams wokring. $model is empty it seams like doesnt get the saved product muodel..
@Rebwar Thanks it works. However would be good to know why the after() method dont works.
But thanks I was researching all affternoon, finally i can keep going..
@Norbertho The after()method can be used with the CreateAction to perform custom actions after a record is created. Alternatively, you can use the afterCreate hook in CreateProduct.php to handle any post-save actions, such as saving related product images or processing additional data associated with the saved record.
// CreateProduct.php
protected function afterCreate(): void
{
// Retrieve uploaded images from the form state
$uploadedImages = $this->form->getState()['images'];
// Save each uploaded image as a related record
foreach ($uploadedImages as $imagePath) {
$this->record->images()->create([
'path' => $imagePath,
]);
}
}