Edit File: Provider.php
<?php namespace App\Models; use App\Enums\ProductDeliveryTypesEnum; use Carbon\Carbon; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\MorphOne; use Spatie\Translatable\HasTranslations; class Provider extends AuthBaseModel { use HasFactory, HasTranslations; const IMAGEPATH = 'providers'; public $translatable = ['store_name', 'store_description']; // protected $appends = ['distance', 'duration']; protected $fillable = [ 'name', 'store_name', 'store_description', 'email', 'country_code', 'phone', 'whatsapp_country_code', 'whatsapp_phone', 'password', 'image', 'logo', 'civil_registration_number', 'identity_number', 'avg_rate', 'active', 'is_blocked', 'tax_number', 'is_approved', 'delivery_method', 'is_available_to_recieve_orders', 'is_notify', 'lang', 'category_id', 'city_id', 'neighborhood_id', 'lat', 'lng', 'map_desc', 'code', 'code_expire', 'minimum_order', 'preparing_time', 'is_busy', ]; protected $casts = [ 'is_available_to_recieve_orders' => 'boolean', 'is_busy' => 'boolean', ]; // Mutators & Accessors public function getLogoAttribute(): string { if ($this->attributes['logo']) { $logo = $this->getImage($this->attributes['logo'], self::IMAGEPATH); } else { $logo = $this->defaultImage(self::IMAGEPATH); } return $logo; } public function getDeliveryMethodTextAttribute() { $value = $this->attributes['delivery_method']; return [ 'value' => (int) $value, 'slug' => ProductDeliveryTypesEnum::from((int) $value)->name, 'text' => __('apis.delivery_method.' . (int) $value), ]; } public function setLogoAttribute($value): void { if (null != $value && is_file($value)) { isset($this->attributes['logo']) ? $this->deleteFile($this->attributes['logo'], self::IMAGEPATH) : ''; $this->attributes['logo'] = $this->uploadAllTyps($value, self::IMAGEPATH); } } // Relations public function mainCategory(): BelongsTo { return $this->belongsTo(Category::class, 'category_id'); } public function city(): BelongsTo { return $this->belongsTo(City::class); } public function neighborhood(): BelongsTo { return $this->belongsTo(Neighborhood::class); } public function subCategories(): BelongsToMany { return $this->belongsToMany(Category::class, 'category_provider', 'provider_id', 'category_id'); } public function orders(): HasMany { return $this->hasMany(Order::class); } public function negotiationOrders(): HasMany { return $this->hasMany(NegotiationOrder::class); } public function orderTransactions(): HasMany { return $this->hasMany(OrderTransaction::class); } public function bankAccount(): MorphOne { return $this->morphOne(BankAccount::class, 'bankable'); } public function products(): HasMany { return $this->hasMany(Product::class); } public function providerWorkTimes(): HasMany { return $this->hasMany(ProviderWorkTime::class); } public function profileUpdates(): HasMany { return $this->hasMany(ProviderUpdate::class); } public function isHolidayThisDay($day): bool { $todayName = Carbon::parse($day)->locale('en')->dayName; return ProviderWorkTime::where('provider_id', $this->id) ->whereHas('day', function ($q) use ($todayName) { $q->whereJsonContains('name->ar', $todayName) ->orWhereJsonContains('name->en', $todayName); })?->first()?->is_holiday ?? false; } public function deliverByThisWay($way): bool { if ($way == $this->delivery_method || $this->delivery_method == ProductDeliveryTypesEnum::Both->value) { return true; } return false; } }
Back to File Manager