Restricting Edit on Some Condition of the Record

Restricting Edit on Some Condition of the Record

2024-10-18 وقت القراءه : 1 دقائق

لفرض أنه نريد إذا تم إستخدام Voucher معين ، أي إنه تمت عمليه شراء من خلال إستخدام الكود الخاص به، أن لا تتاح عمليه التعديل عليه.


كما نلاحظ أنه جميع Vouchers تم إستخدامها، لذلك سنضيف شرط أن أي count اكبر من 0 أن لا تتاح عمليه التعديل عليه

نذهب للملف EditVoucher.php ونضيف الدالة beforeFill

class EditVoucher extends EditRecord
{
    protected static string $resource = VoucherResource::class;


    protected function beforeFill()
    {
        if($this->record->payments()->exists()){
            $this->redirect($this->getResource()::getUrl('index'));
        }
    }
    protected function getHeaderActions(): array
    {
        return [
            Actions\DeleteAction::make(),
        ];
    }
}

للتوضيح

هنا نستخدم الدالة beforeFill ونضع بداخلها الشرط حيث تم تحديد هل يوجد علاقه  payments على هذا العنصر ، إذا كانت موجوده اي أكثر من 0 عدد عمليات الشراء ان يتم إعاده توجيه المستخدم للصفحة الرئيسيه، أما إذا كان لا يوجد علاقه، اي لا يوجد عمليات شراء بإستخدام هذا الكود أن يتاح له التعديل.


لكن هنا ومن ناحية user ui يجب أن نظهر للمستخدم رسالة أنه لا يسمح له بالعديل على هذا العنصر

وللقيام بذلك نضيف دالة Notification إلى الدالة beforeFill

protected function beforeFill()
{
    if($this->record->payments()->exists()){
        Notification::make()
            ->title('Error')
            ->body('You cannot edit a voucher')
            ->danger()
            ->send();
        $this->redirect($this->getResource()::getUrl('index'));
    }
}

كما نلاحظ أننا قمنا بتمرير النوع وهو danger وكذلك الرسالة إلتي نريد إظهارها إلى notify

إضافة تعليق
Loading...