83 lines
2.1 KiB
PHP
83 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Concentration;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Spatie\MediaLibrary\HasMedia;
|
|
use Spatie\MediaLibrary\InteractsWithMedia;
|
|
use Spatie\Sluggable\HasSlug;
|
|
use Spatie\Sluggable\SlugOptions;
|
|
use Veelasky\LaravelHashId\Eloquent\HashableId;
|
|
|
|
class Perfume extends Model implements HasMedia
|
|
{
|
|
use HasFactory, HashableId, HasSlug, InteractsWithMedia, SoftDeletes;
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::addGlobalScope('alphabetical', function ($builder) {
|
|
$builder->orderBy('name', 'asc');
|
|
});
|
|
}
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'concentration' => Concentration::class,
|
|
'cost_price' => 'int',
|
|
'sale_price' => 'int',
|
|
'views' => 'int',
|
|
];
|
|
}
|
|
|
|
public function getSlugOptions(): SlugOptions
|
|
{
|
|
return SlugOptions::create()
|
|
->generateSlugsFrom('name')
|
|
->saveSlugsTo('slug');
|
|
}
|
|
|
|
public function brand(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Brand::class);
|
|
}
|
|
|
|
public function categories(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Category::class);
|
|
}
|
|
|
|
public function items(): MorphMany
|
|
{
|
|
return $this->morphMany(OrderItem::class, 'orderable');
|
|
}
|
|
|
|
public function outlets(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Outlet::class)->withPivot('stock');
|
|
}
|
|
|
|
public function purchaseItems(): MorphMany
|
|
{
|
|
return $this->morphMany(PurchaseItem::class, 'purchasable');
|
|
}
|
|
|
|
public function restockItems(): MorphMany
|
|
{
|
|
return $this->morphMany(RestockItem::class, 'restockable');
|
|
}
|
|
|
|
public function warehouses(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Warehouse::class)->withPivot('stock');
|
|
}
|
|
}
|