is there a way to mutate data on createOptionForm in Select input.

i want to attach the creator_id before saving the form instead of having the a readonly field. Forms\Components\Select::make('payment_method_id') ->label('Payment Method') ->relationship(name: 'paymentMethod', titleAttribute: 'name') ->required() ->createOptionForm([ Forms\Components\TextInput::make('name')->required(), Forms\Components\Textarea::make('description')->helperText('more about channel, eg bank acc nu')->required(), Forms\Components\TextInput::make('creator_id')->default(Auth::id())->readOnly(), ]),
4 Replies
tuto1902
tuto190216mo ago
Do you need to mutate the data before save or before show?
jangili1
jangili1OP16mo ago
before save.
tuto1902
tuto190216mo ago
I couldn’t find any other way to do it via filament. But, you could probably use model observers to perform a “before save” action. you can create an observer using php artisan make:observer --model=PaymentMethod and then only use the saving event
<?php

namespace App\Observers;

use App\Models\User;
use Illuminate\Support\Facades\Auth;

class PaymentMethodObserver
{
/**
* Handle the User "saving" event.
*/
public function saving(PaymentMethod $paymentMethod): void
{
$paymentMethod->creator_id = Auth::id();
}
}
<?php

namespace App\Observers;

use App\Models\User;
use Illuminate\Support\Facades\Auth;

class PaymentMethodObserver
{
/**
* Handle the User "saving" event.
*/
public function saving(PaymentMethod $paymentMethod): void
{
$paymentMethod->creator_id = Auth::id();
}
}
Then, in App\Providers\EventServiceProvider
use App\Models\PaymentMethod;
use App\Observers\PaymentMethodObserver;

/**
* Register any events for your application.
*/
public function boot(): void
{
PaymetMethod::observe(PaymentMethodObserver::class);
}
use App\Models\PaymentMethod;
use App\Observers\PaymentMethodObserver;

/**
* Register any events for your application.
*/
public function boot(): void
{
PaymetMethod::observe(PaymentMethodObserver::class);
}
jangili1
jangili1OP16mo ago
thanks. i thought filament had a way but observer will work just fine. Thankyou.
Want results from more Discord servers?
Add your server