Edit File: UpdateProductRequest.php
<?php namespace App\Http\Requests\Api\Provider\Product; use App\Http\Requests\Api\BaseApiRequest; use App\Models\ProductImage; class UpdateProductRequest extends BaseApiRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array<string, mixed> */ public function rules() { return [ 'name' => 'required|array|size:' . count(config('translatable.locales')), 'name.*' => 'string|max:255', 'description' => 'required|array|size:' . count(config('translatable.locales')), 'description.*' => 'string|max:1000', 'has_price' => 'required|boolean', 'price' => 'required_if:has_price,1,true|numeric|gt:0', 'price_after_discount' => 'nullable|numeric|gt:0|lt:price', 'category_id' => ['required', 'exists:categories,id', function ($attribute, $value, $fail) { $category = \App\Models\Category::where('id', $value)->first()->level; if ($category != 3) { $fail(__('apis.category_not_able_hold_products')); } }], 'images' => 'nullable|array', 'images.*' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg,webp|max:2048', ]; } public function attributes() { return [ 'has_price' => __('apis.product_type'), 'category_id' => __('apis.sub_category'), ]; } public function messages() { return [ 'price.required_if' => __('apis.product_has_priced_required'), ]; } public function withValidator($validator) { $validator->after(function ($validator) { if ($this->has_price && !$this->price_after_discount) { $validator->errors()->add('price_after_discount', __('apis.price_after_discount_required')); } if (empty($this->images) && ProductImage::where('product_id', $this->id)->doesntExist()) { $validator->errors()->add('images', __('apis.product_must_have_images')); } }); } }
Back to File Manager