64 lines
1.3 KiB
PHP
64 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class Product extends Model
|
|
{
|
|
/** @use HasFactory<\Database\Factories\ProductFactory> */
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $fillable = [
|
|
'category_id',
|
|
'brand_id',
|
|
'name',
|
|
'slug',
|
|
'sku',
|
|
'short_description',
|
|
'description',
|
|
'price',
|
|
'stock_quantity',
|
|
'active',
|
|
'published_at',
|
|
];
|
|
|
|
/**
|
|
* Get the attributes that should be cast.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'price' => 'decimal:2',
|
|
'active' => 'boolean',
|
|
'published_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
public function category(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Category::class);
|
|
}
|
|
|
|
public function brand(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Brand::class);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function active(Builder $query) : void
|
|
{
|
|
$query->where('active', 1);
|
|
}
|
|
}
|