[ 'source' => 'name', 'onUpdate' => true ] ]; } public function getStatusAttribute(): string { return $this->attributes['status'] == 0 ? 'Inactive' : 'Active'; } public function scopeActive($query) { return $query->whereStatus(true); } public function scopeHasQuantity($query) { return $query->where('quantity', '>', 0); } public function category(){ return $this->belongsTo(Category::class); } public function tags(){ return $this->belongsToMany(Tag::class, 'product_tags'); } public function media(): MorphMany { return $this->morphMany(Media::class, 'mediable'); } public function firstMedia(): MorphOne { return $this->morphOne(Media::class, 'mediable') ->orderBy('file_sort', 'asc'); } public function reviews() { return $this->hasMany(Review::class); } public function approvedReviews() { return $this->hasMany(Review::class)->whereStatus(1); } public function ratings() { return $this->hasMany(Rating::class); } public function rate() { return $this->ratings->isNotEmpty() ? $this->ratings()->sum('value') / $this->ratings()->count() : 0; } }