- Added activity logging functionality to multiple models including CuttingMaterial, Employee, Invoice, and more. - Implemented `getActivitylogOptions` method to configure logging behavior. - Updated composer dependencies to include `spatie/laravel-activitylog`. - Created a custom Activity model extending Spatie's Activity model. - Added configuration file for activity logging settings.
104 lines
2.6 KiB
PHP
104 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\ProductStatus;
|
|
use Illuminate\Database\Eloquent\Attributes\Appends;
|
|
use Illuminate\Database\Eloquent\Attributes\Guarded;
|
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
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\Activitylog\Support\LogOptions;
|
|
use Spatie\Activitylog\Models\Concerns\LogsActivity;
|
|
use Spatie\Sluggable\Attributes\Sluggable;
|
|
|
|
#[Appends(['formatted_name', 'status_label'])]
|
|
#[Guarded(['id'])]
|
|
#[Sluggable(from: 'name', to: 'slug')]
|
|
class Product extends Model
|
|
{
|
|
use HasFactory, LogsActivity, SoftDeletes;
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'status' => ProductStatus::class,
|
|
'is_featured' => 'boolean',
|
|
];
|
|
}
|
|
|
|
public function getActivitylogOptions(): LogOptions
|
|
{
|
|
return LogOptions::defaults()
|
|
->logAll()
|
|
->logOnlyDirty()
|
|
->dontLogEmptyChanges();
|
|
}
|
|
|
|
protected function formattedName(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => ucfirst($this->name),
|
|
);
|
|
}
|
|
|
|
protected function statusLabel(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->status?->label(),
|
|
);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function active(Builder $query): void
|
|
{
|
|
$query->where('status', ProductStatus::ACTIVE);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function draft(Builder $query): void
|
|
{
|
|
$query->where('status', ProductStatus::DRAFT);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function featured(Builder $query): void
|
|
{
|
|
$query->where('is_featured', true);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function inactive(Builder $query): void
|
|
{
|
|
$query->where('status', ProductStatus::INACTIVE);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function pending(Builder $query): void
|
|
{
|
|
$query->where('status', ProductStatus::PENDING);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function rejected(Builder $query): void
|
|
{
|
|
$query->where('status', ProductStatus::REJECTED);
|
|
}
|
|
|
|
public function categories(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Category::class, 'product_categories')
|
|
->using(ProductCategory::class);
|
|
}
|
|
|
|
public function productVariants(): HasMany
|
|
{
|
|
return $this->hasMany(ProductVariant::class);
|
|
}
|
|
}
|