87 lines
1.9 KiB
PHP
87 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Cviebrock\EloquentSluggable\Sluggable;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne;
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
class Product extends Model
|
|
{
|
|
use HasFactory, Sluggable;
|
|
|
|
protected $guarded = ['id', 'updated_at', 'created_at'];
|
|
|
|
/**
|
|
* Return the sluggable configuration array for this model.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function sluggable(): array
|
|
{
|
|
return [
|
|
'slug' => [
|
|
'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;
|
|
}
|
|
|
|
}
|