$activeDiscounts * @property-read int|null $active_discounts_count * @mixin \Eloquent */ class Product extends Model implements HasMedia { use SoftDeletes, InteractsWithMedia, Taggable, Metable, HasFactory,HasTranslations; protected $guarded = []; protected $appends = ['url']; public $translatable = ['name','excerpt','description']; public function getTitle() { return $this->name . getSetting('prefix') . $this->id; } public function comments() { return $this->morphMany(Comment::class, 'commentable'); } public function approved_comments() { return $this->morphMany(Comment::class, 'commentable')->where('status', 1)->whereNull('sub_comment_id'); } public function categories() { return $this->belongsToMany(Cat::class); } public function category() { return $this->belongsTo(Cat::class, 'cat_id', 'id'); } public function getRouteKeyName() { return 'slug'; } public function getCode() { if ($this->sku != '') return $this->sku; else return $this->id; } public function registerMediaConversions(Media $media = null): void { $size = explode('x', config('app.thumbnail_size')); $this->addMediaConversion('product-image') ->width(1200) // ->height(600) // ->crop(Manipulations::CROP_CENTER, 1200, 600) ->optimize() ->sharpen(10); $this->addMediaConversion('product-thumb') ->width($size[0]) ->height($size[1]) ->crop(Manipulations::CROP_CENTER, $size[0], $size[1]) ->optimize() ->sharpen(10); } public function thumbUrl() { if ($this->getMedia()->count() > 0) { return $this->getMedia()[$this->image_index]->getUrl('product-thumb'); } else { return asset('/images/logo.png'); } } public function thumbUrl2() { if ($this->getMedia()->count() > 0 && isset($this->getMedia()[1])) { return $this->getMedia()[1]->getUrl('product-thumb'); } else { return asset('/images/logo.png'); } } public function imgurl() { if ($this->getMedia()->count() > 0) { return $this->getMedia()[$this->image_index]->getUrl(); } else { return asset('/images/logo.png'); } } public function getUrlAttribute() { return route('product', ['pro' => $this->slug]); } public function quantities() { return $this->hasMany(Quantity::class, 'product_id'); } public function prices() { return $this->hasMany(Price::class, 'product_id', 'id'); } public function prices_history() { return $this->prices()->orderByDesc('id')->limit(20); } public function discounts() { return $this->hasMany(Discount::class, 'product_id', 'id'); } public function activeDiscounts() { return $this->hasMany(Discount::class, 'product_id', 'id')->where(function ($query) { $query->where('expire', '>=', date('Y-m-d')) ->orWhereNull('expire'); }); } public function discountWithSign() { if ($this->activeDiscounts()->count() > 0) { $discount = $this->activeDiscounts()->first(); if ($discount->type == 'price') { return ' - ' . $discount->amount; } else { return ' * ' . (( 100 - $discount->amount ) / 100); } } else { return null; } } public function getPurePrice() { if ($this->activeDiscounts()->whereNull('code')->count() > 0) { $d = $this->activeDiscounts()->whereNull('code')->orderBy('id', 'desc')->first(); if ($d->type == 'percent') { $price = $this->price - ($this->price * ($d->amount / 100)); return $price; } else { return $this->price - $d->amount; } } return $this->price; } /** * with default * @param $def * @return float|\Illuminate\Database\Eloquent\HigherOrderBuilderProxy|int|mixed|null */ public function getPurePriceDef($def) { if ($this->activeDiscounts()->whereNull('code')->count() > 0) { $d = $this->activeDiscounts()->whereNull('code')->orderBy('id', 'desc')->first(); if ($d->type == 'percent') { $price = $def - ($def * ($d->amount / 100)); return $price; } else { return $def - $d->amount; } } return $def; } public function getOldPrice() { if ($this->getPurePrice() == 0) { return __('Call us!'); } return number_format($this->price) . ' ' . config('app.currency_type'); } public function getPrice() { if ($this->getPurePrice() == 0) { return __('Call us!'); } return number_format($this->getPurePrice()) . ' ' . config('app.currency_type'); } public function quesions() { return $this->hasMany(Question::class); } public function quesions_asnwered() { return $this->hasMany(Question::class)->where('status', 1); } function hasDiscount() { return $this->discounts()->where('expire', '>', date('Y-m-d'))->count() > 0; } public function isFav() { if (auth('customer')->check()) { return \auth('customer')->user()->products()->where('product_id', $this->id)->exists(); } else { return false; } } }