F
Filament10mo ago
Nathan

SpatieMediaLibraryFileUpload inside action how to attach to new model

I have an Order Resource in infolist i have an action that creates a payment record attached to current order, but when i added SpatieMediaLibraryFileUpload field to this form it saves the image on the order instead of the payment. Is there a way to tell it to attach to the newly created payment record. Here is my code:
Action::make('addPayment')
->icon('heroicon-m-plus')
->iconButton()
->form([
Grid::make(2)
->schema([
TextInput::make('amount')
->label('Amount')
->prefix('$')
->numeric()
->required(),
DatePicker::make('created_at')
->label('Date')
->default(now())
->required(),
Select::make('payment_method')
->label('Payment Method')
->options(PaymentMethod::toArray())
->required(),
TextInput::make('payment_ref_number')
->label('Reference Number'),
Textarea::make('payment_memo')
->columnSpanFull()
->label('Memo')
->rows(2),
SpatieMediaLibraryFileUpload::make('attachments')
->appendFiles()
->openable()
->collection('resource_files')
->multiple()
->reorderable(),
])
])
->action(function (Order $record, array $data) {
$payment = $record
->payments()
->create([
'amount' => $data['amount'],
'payment_method' => $data['payment_method'],
'payment_ref_number' => $data['payment_ref_number'],
'payment_memo' => $data['payment_memo'],
'created_at' => $data['created_at'],
]);

Notification::make()
->title('Payment Added')
->success()
->send();
})
Action::make('addPayment')
->icon('heroicon-m-plus')
->iconButton()
->form([
Grid::make(2)
->schema([
TextInput::make('amount')
->label('Amount')
->prefix('$')
->numeric()
->required(),
DatePicker::make('created_at')
->label('Date')
->default(now())
->required(),
Select::make('payment_method')
->label('Payment Method')
->options(PaymentMethod::toArray())
->required(),
TextInput::make('payment_ref_number')
->label('Reference Number'),
Textarea::make('payment_memo')
->columnSpanFull()
->label('Memo')
->rows(2),
SpatieMediaLibraryFileUpload::make('attachments')
->appendFiles()
->openable()
->collection('resource_files')
->multiple()
->reorderable(),
])
])
->action(function (Order $record, array $data) {
$payment = $record
->payments()
->create([
'amount' => $data['amount'],
'payment_method' => $data['payment_method'],
'payment_ref_number' => $data['payment_ref_number'],
'payment_memo' => $data['payment_memo'],
'created_at' => $data['created_at'],
]);

Notification::make()
->title('Payment Added')
->success()
->send();
})
2 Replies
BOT_Larry
BOT_Larry10mo ago
I have something like this working with the default FileUpload: Inside the action:
$record->addMedia(Storage::disk('public')->path($data['attachments']));
// ->toMediaCollection($data['file_usage']); //optional
$record->addMedia(Storage::disk('public')->path($data['attachments']));
// ->toMediaCollection($data['file_usage']); //optional
Nathan
NathanOP10mo ago
I want to keep all my images under the Spatie Media Library
Want results from more Discord servers?
Add your server