MIF_E31210863/app/Models/Product.php

74 lines
1.5 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');
}
}