F
Filament6mo ago
vas

update related model value

class Order extends Model
{
protected $fillable = [
'user_id',
'payment_gateway',
'session_id',
'amount',
'status',
];
public function payment()
{
return $this->hasOne(Payment::class);
}
}



class Payment extends Model
{

protected $fillable = [
'order_id',
'amount',
'status',
];

public function order()
{
return $this->belongsTo(Order::class);
}
}


class PaymentResource extends Resource
{
public static function form(Form $form): Form
{
return $form
->schema([

Forms\Components\Select::make('status')
->required()
->options([
'pending' => PaymentStatusEnum::PENDING->value,
'paid' => PaymentStatusEnum::PAID->value,
'declined' => PaymentStatusEnum::DECLINED->value,
'failed' => PaymentStatusEnum::FAILED->value
])->label('Stare'),



}
}
class Order extends Model
{
protected $fillable = [
'user_id',
'payment_gateway',
'session_id',
'amount',
'status',
];
public function payment()
{
return $this->hasOne(Payment::class);
}
}



class Payment extends Model
{

protected $fillable = [
'order_id',
'amount',
'status',
];

public function order()
{
return $this->belongsTo(Order::class);
}
}


class PaymentResource extends Resource
{
public static function form(Form $form): Form
{
return $form
->schema([

Forms\Components\Select::make('status')
->required()
->options([
'pending' => PaymentStatusEnum::PENDING->value,
'paid' => PaymentStatusEnum::PAID->value,
'declined' => PaymentStatusEnum::DECLINED->value,
'failed' => PaymentStatusEnum::FAILED->value
])->label('Stare'),



}
}
any way to update the orders status to paid when selecting the payment as paid ? thanks
3 Replies
vas
vas6mo ago
yea something like after method it works but it puts in wrong values
->after(function (Payment $payment) {

if ($payment->status === PaymentStatusEnum::PAID->value) {
// Update the corresponding order status to 'paid'
$payment->order->update(['status' => 'paid']);
}

elseif ($payment->status === PaymentStatusEnum::UNPAID->value) {
// Update the corresponding order status to 'paid'
$payment->order->update(['status' => 'unpaid']);
}


elseif ($payment->status === PaymentStatusEnum::PENDING->value) {
// Update the corresponding order status to 'paid'
$payment->order->update(['status' => 'pending']);
}

elseif ($payment->status === PaymentStatusEnum::DECLINED->value) {
// Update the corresponding order status to 'paid'
$payment->order->update(['status' => 'declined']);
}

elseif ($payment->status === PaymentStatusEnum::FAILED->value) {
// Update the corresponding order status to 'paid'
$payment->order->update(['status' => 'failed']);
}




})
->after(function (Payment $payment) {

if ($payment->status === PaymentStatusEnum::PAID->value) {
// Update the corresponding order status to 'paid'
$payment->order->update(['status' => 'paid']);
}

elseif ($payment->status === PaymentStatusEnum::UNPAID->value) {
// Update the corresponding order status to 'paid'
$payment->order->update(['status' => 'unpaid']);
}


elseif ($payment->status === PaymentStatusEnum::PENDING->value) {
// Update the corresponding order status to 'paid'
$payment->order->update(['status' => 'pending']);
}

elseif ($payment->status === PaymentStatusEnum::DECLINED->value) {
// Update the corresponding order status to 'paid'
$payment->order->update(['status' => 'declined']);
}

elseif ($payment->status === PaymentStatusEnum::FAILED->value) {
// Update the corresponding order status to 'paid'
$payment->order->update(['status' => 'failed']);
}




})
php but the values are random for order->status idk why like if i set it to paid order will be pending , or if i set to declied will be paid etc
Tobias Platen
Tobias Platen6mo ago
The after() method of the Select form classes is related to validation. You need to use the afterSave() and afterCreate() methods in the pages. In my opinion, this context is better suited for a model observer. If you cast the attributes correctly in the models and use the Filament methods in your enum, you can avoid using ->value in most places. To access the model in the lifecycle hooks, you can use $this->record https://filamentphp.com/docs/3.x/panels/resources/editing-records#lifecycle-hooks https://filamentphp.com/docs/3.x/panels/resources/creating-records#lifecycle-hooks