has('discounts')) { foreach ($this->get('discounts') as $discountRequest) { if ($discountRequest['discount_type_id'] === 1) { checkPrice($attribute, $value, $fail); } else if ($discountRequest['discount_type_id'] === 2) { checkPercent($attribute, $value, $fail); } else { $fail("The {$attribute} is invalid."); } } } else { if ($this->get('discount_type_id') === 1) { checkPrice($attribute, $value, $fail); } else if ($this->get('discount_type_id') === 2) { checkPercent($attribute, $value, $fail); } else { $fail("The {$attribute} is invalid."); } } } /** * Get the validation rules that apply to the request. */ public function rules(): array { $actionMethod = $this->route()->getActionMethod(); $rules = []; // update single if ($actionMethod == 'update') { $rules = [ 'id' => 'required|exists:' . (new Discount())->getTable(), 'discount_type_id' => 'exists:' . (new DiscountType)->getTable() . ',id', 'value' => function (string $attribute, $value, Closure $fail) { $this->checkValue($attribute, $value, $fail); }, ]; } if ($actionMethod == 'create') { $rules = [ 'discount_type_id' => 'exists:' . (new DiscountType)->getTable() . ',id', 'value' => function (string $attribute, $value, Closure $fail) { $this->checkValue($attribute, $value, $fail); }, ]; } // multiple if ($actionMethod == 'updates') { $rules = [ 'discounts.*.id' => 'required|exists:' . (new Discount())->getTable(), 'discounts.*.discount_type_id' => 'exists:' . (new DiscountType)->getTable() . ',id', 'discounts.*.value' => function (string $attribute, $value, Closure $fail) { $this->checkValue($attribute, $value, $fail); }, ]; } if ($actionMethod == 'creates') { $rules = [ 'discounts.*.id' => 'required|exists:' . (new Discount())->getTable(), 'discounts.*.discount_type_id' => 'exists:' . (new DiscountType)->getTable() . ',id', 'discounts.*.value' => function (string $attribute, $value, Closure $fail) { $this->checkValue($attribute, $value, $fail); }, ]; } // single if ($actionMethod == 'delete') { $rules = [ 'id' => 'required|exists:' . (new Discount())->getTable() ]; } // multiple if ($actionMethod == 'deletes') { $rules = [ 'discounts.*.id' => 'required|exists:' . (new Discount())->getTable() ]; } return array_merge([ 'status' => 'in:0,1,true,false', 'active_date' => 'size:10', // as timestamp unix 'expiry' => 'size:10', // as timestamp unix 'date_used' => 'size:10', // as timestamp unix 'discounts.*.status' => 'in:0,1,true,false', ], $rules); } /** * Determine if the user is authorized to make this request. */ public function authorize(): bool { return true; } }