ekart/backend/app/Models/Product.php

40 lines
924 B
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;
class Product extends Model
{
protected $fillable = [
'title',
'slug',
'actual_price',
'list_price',
'description',
'product_category_id',
];
public function category(): BelongsTo
{
return $this->belongsTo(ProductCategory::class, 'product_category_id');
}
public function images(): HasMany
{
return $this->hasMany(ProductImage::class, 'product_id', 'id');
}
protected static function booted(): void
{
static::saving(function ($product) {
if (empty($product->slug) || $product->isDirty('title')) {
$product->slug = Str::slug($product->title);
}
});
}
}