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 { $this->addMediaConversion('product-image') ->width(1200) // ->height(600) // ->crop(Manipulations::CROP_CENTER, 1200, 600) ->optimize() ->sharpen(10); $this->addMediaConversion('product-thumb') ->width(600) ->height(600) ->crop(Manipulations::CROP_CENTER, 600, 600) ->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 getPurePrice() { if ($this->discounts()->whereNull('code')->count() > 0) { $d = $this->discounts()->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->discounts()->whereNull('code')->count() > 0) { $d = $this->discounts()->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 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', '>', \DB::raw('NOW()'))->count() > 1; } public function isFav() { if (auth('customer')->check()) { return \auth('customer')->user()->products()->where('product_id', $this->id)->exists(); } else { return false; } } }