File "CouponUpdateRequest.php"

Full Path: /home/clickysoft/public_html/jmapi5.clickysoft.net/app/Http/Requests/Admin/CouponUpdateRequest.php
File size: 1.14 KB
MIME-type: text/x-php
Charset: utf-8

<?php

namespace App\Http\Requests\Admin;

use App\Models\Coupon;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;

class CouponUpdateRequest extends FormRequest
{
    public function authorize(): bool
    {
        return Gate::allows('coupon_edit');
    }

    public function rules(): array
    {
        return [
            'code' => 'required|string|max:30|unique:coupons,code,'.request()->route('coupon')->id,
            'discount_type' => 'required|in:' . implode(',', array_keys(Coupon::DISCOUNT_TYPE)),
            'discount_value' => [
                'required',
                'numeric',
                'min:0',
                'not_in:0',
                function ($attribute, $value, $fail) {
                    if ($this->discount_type === 'percentage' && $value > 100) {
                        $fail('The discount value cannot exceed 100% when the discount type is percentage.');
                    }
                },
            ],
            'expiry_date' => 'nullable|date|after:yesterday',
            'number_of_usage' => 'nullable|numeric',
            'status' => 'required|boolean',
        ];
    }
}