Edit File: UpdateProfileRequest.php
<?php namespace App\Http\Requests\Api\Provider\Profile; use App\Enums\ProductDeliveryTypesEnum; use App\Http\Requests\BaseRequest; use App\Models\Product; use Illuminate\Validation\Rule; class UpdateProfileRequest extends BaseRequest { public function rules() { $id = auth()->id(); $bankAccountId = auth()->user()->bankAccount->id; return [ // General Information for Auth 'image' => 'nullable|image|mimes:jpeg,png,jpg,svg,webp|max:2048', 'logo' => 'nullable|image|mimes:jpeg,png,jpg,svg,webp|max:2048', 'name' => 'required|string|max:200', 'preparing_time' => 'required|string|max:200', // StoreRequest Information 'identity_number' => ['required', 'numeric', 'digits_between:10,16', Rule::unique('providers', 'identity_number')->whereNull('deleted_at')->ignore($id)], 'civil_registration_number' => ['required', 'numeric', 'digits_between:10,16', Rule::unique('providers', 'civil_registration_number')->whereNull('deleted_at')->ignore($id)], 'tax_number' => ['required', 'numeric', 'digits_between:10,16', Rule::unique('providers', 'tax_number')->whereNull('deleted_at')->ignore($id)], 'store_name' => 'required|array', 'store_name.*' => 'required|max:200', 'store_description' => 'required|array', 'store_description.*' => 'required|max:500', 'whatsapp_country_code' => 'required|numeric|digits_between:2,5', 'whatsapp_phone' => [ 'required', 'numeric', 'digits_between:9,10', Rule::unique('providers', 'whatsapp_phone') ->whereNull('deleted_at')->ignore($id) ], 'category_id' => [ 'required', 'numeric', Rule::exists('categories', 'id')->whereNull('parent_id') ], 'sub_categories' => 'required|array', 'sub_categories.*' => [ 'required', 'numeric', 'exists:categories,id', Rule::exists('categories', 'id') ->whereNotNull('parent_id')->where('parent_id', $this->category_id) ], // Location Information 'city_id' => 'required|numeric|exists:cities,id', 'neighborhood_id' => 'required|numeric|exists:neighborhoods,id', 'lat' => 'required|numeric', 'lng' => 'required|numeric', 'map_desc' => 'nullable|string|max:255', // bank account 'minimum_order' => 'required|numeric|min:1|max:100000', 'bank_name' => 'required|string|max:255', 'account_name' => 'required|string|max:255', 'account_number' => [ 'required', 'numeric', Rule::unique('bank_accounts', 'account_number') ->whereNull('deleted_at')->ignore($bankAccountId), 'digits_between:10,20' ], 'iban' => [ 'required', 'string', 'regex:/(SA)[0-9]{22}/', Rule::unique('bank_accounts', 'iban') ->whereNull('deleted_at')->ignore($bankAccountId), ], 'bank_account_image' => 'nullable|file|mimes:jpeg,png,jpg,pdf|max:2048', // end bank account 'delivery_method' => 'required|numeric|in:' . implode(',', array_column(ProductDeliveryTypesEnum::cases(), 'value')), ]; } public function prepareForValidation(): void { $this->merge([ 'phone' => fixPhone($this->phone), 'country_code' => fixPhone($this->country_code), 'whatsapp_phone' => fixPhone($this->whatsapp_phone), 'whatsapp_country_code' => fixPhone($this->whatsapp_country_code), ]); } // public function withValidator($validator) // { // $validator->after(function ($validator) { // if ($this->has('sub_categories') && $this->has('category_id')) { // $removedCategories = array_diff( // array_merge(auth()->user()->subCategories->pluck('id')->toArray(), [auth()->user()->category_id]), // array_merge($this->sub_categories, [$this->category_id]) // ); // $parentCategoriesIdsForProviderProducts = Product::where('provider_id', auth()->id()) // ->with('category.parent.parent') // ->get() // ->map(function ($product) { // $category = $product->category; // $parents = []; // while ($category && $category->parent_id) { // $parents[] = $category->parent_id; // $category = $category->parent; // } // return $parents; // }) // ->flatten() // ->unique() // ->values() // ->toArray(); // $affectedChanges = array_intersect($removedCategories, $parentCategoriesIdsForProviderProducts); // if (!empty($affectedChanges)) { // $validator->errors()->add('sub_categories', __('apis.cannot_made_this_change_for_categories_because_they_have_products')); // } // } // }); // } }
Back to File Manager