69 lines
1.8 KiB
PHP
69 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Concerns\InteractsWithActivityLog;
|
|
use Illuminate\Database\Eloquent\Attributes\Guarded;
|
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Spatie\Sluggable\Attributes\Sluggable;
|
|
|
|
/**
|
|
* @method static \Illuminate\Database\Eloquent\Builder active()
|
|
* @method static \Illuminate\Database\Eloquent\Builder inactive()
|
|
*/
|
|
#[Guarded(['id'])]
|
|
#[Sluggable(from: 'name', to: 'slug')]
|
|
class Product extends Model
|
|
{
|
|
use HasFactory, InteractsWithActivityLog, SoftDeletes;
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'is_active' => 'boolean',
|
|
];
|
|
}
|
|
|
|
public function categories(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Category::class, 'product_categories');
|
|
}
|
|
|
|
public function variants(): HasMany
|
|
{
|
|
return $this->hasMany(ProductVariant::class);
|
|
}
|
|
|
|
#[Scope]
|
|
public function active(Builder $query): void
|
|
{
|
|
$query->where('is_active', true);
|
|
}
|
|
|
|
#[Scope]
|
|
public function inactive(Builder $query): void
|
|
{
|
|
$query->where('is_active', false);
|
|
}
|
|
|
|
public static function getActiveWithVariantsAndCategories(): Collection
|
|
{
|
|
return self::query()
|
|
->active()
|
|
->with([
|
|
'categories',
|
|
'variants' => fn ($query) => $query
|
|
->with('media')
|
|
->orderBy('created_at'),
|
|
])
|
|
->get();
|
|
}
|
|
}
|