104 lines
3.1 KiB
PHP
104 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property string $title
|
|
* @property string|null $slug
|
|
* @property string $description
|
|
* @property numeric $actual_price
|
|
* @property numeric $list_price
|
|
* @property int $product_category_id
|
|
* @property Carbon|null $created_at
|
|
* @property Carbon|null $updated_at
|
|
* @property bool $is_active
|
|
* @property-read Collection<int, Cart> $carts
|
|
* @property-read int|null $carts_count
|
|
* @property-read ProductCategory|null $category
|
|
* @property-read Collection<int, User> $favoritedBy
|
|
* @property-read int|null $favorited_by_count
|
|
* @property-read Collection<int, ProductImage> $images
|
|
* @property-read int|null $images_count
|
|
*
|
|
* @method static Builder<static>|Product active()
|
|
* @method static Builder<static>|Product newModelQuery()
|
|
* @method static Builder<static>|Product newQuery()
|
|
* @method static Builder<static>|Product query()
|
|
* @method static Builder<static>|Product whereActualPrice($value)
|
|
* @method static Builder<static>|Product whereCreatedAt($value)
|
|
* @method static Builder<static>|Product whereDescription($value)
|
|
* @method static Builder<static>|Product whereId($value)
|
|
* @method static Builder<static>|Product whereIsActive($value)
|
|
* @method static Builder<static>|Product whereListPrice($value)
|
|
* @method static Builder<static>|Product whereProductCategoryId($value)
|
|
* @method static Builder<static>|Product whereSlug($value)
|
|
* @method static Builder<static>|Product whereTitle($value)
|
|
* @method static Builder<static>|Product whereUpdatedAt($value)
|
|
*
|
|
* @mixin \Eloquent
|
|
*/
|
|
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');
|
|
}
|
|
|
|
public function favoritedBy(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(User::class, 'favorite_products');
|
|
}
|
|
|
|
public function carts()
|
|
{
|
|
return $this->belongsToMany(Cart::class);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function active(Builder $query): Builder
|
|
{
|
|
return $query->where('is_active', true);
|
|
}
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::saving(function ($product) {
|
|
if (empty($product->slug) || $product->isDirty('title')) {
|
|
$product->slug = Str::slug($product->title);
|
|
}
|
|
});
|
|
}
|
|
|
|
protected function casts()
|
|
{
|
|
return [
|
|
'is_active' => 'boolean',
|
|
];
|
|
}
|
|
}
|