ldc-gen-game-web/app/Models/Range.php

98 lines
2.4 KiB
PHP
Raw Permalink Normal View History

<?php
2026-10-02 12:09:46 +02:00
declare(strict_types=1);
namespace App\Models;
2023-08-22 13:38:38 +02:00
use App\Lib\Helpers\FileStorageHelper;
2026-10-02 12:09:46 +02:00
use App\Traits\Models\HasActivityLog;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2023-12-12 12:55:05 +01:00
use Illuminate\Database\Eloquent\Relations\HasMany;
2026-10-02 12:09:46 +02:00
use Illuminate\Support\Carbon;
/**
* Range.
*
2026-10-02 12:09:46 +02:00
* @property string $name Name
* @property string $picture Path of the picture
* @property string $picture_alt Alt text for the picture
* @property string $picture_title Title text for the picture
* @property-read Carbon $created_at Creation Date.
* @property-read Carbon $updated_at Last update Date.
* @property-read Collection|Product[] $products
* Associated products.
2024-02-15 15:53:36 +01:00
* @property-read \App\Models\ActivityLog $activityLogs Activities logs One-to-many relationship.
*/
class Range extends Model
{
2026-10-02 12:09:46 +02:00
use HasActivityLog;
use HasFactory;
/**
* The attributes that are mass assignable.
*
2026-10-02 12:09:46 +02:00
* @var list<string>
*/
2026-10-02 12:09:46 +02:00
#[\Override]
2023-08-22 13:38:38 +02:00
protected $fillable = [
'name',
'picture',
'picture_alt',
'picture_title',
2023-08-22 13:38:38 +02:00
];
// * EVENTS
/**
* Perform any actions required after the model boots.
*/
2026-10-02 12:09:46 +02:00
protected static function booted(): void
2023-08-22 13:38:38 +02:00
{
2026-10-02 12:09:46 +02:00
static::creating(function (self $model): void {
2023-08-22 13:38:38 +02:00
self::manageFields($model);
});
2026-10-02 12:09:46 +02:00
static::updating(function (self $model): void {
2023-08-22 13:38:38 +02:00
self::manageFields($model);
});
2026-10-02 12:09:46 +02:00
static::updated(function (self $model): void {
2023-08-22 13:38:38 +02:00
FileStorageHelper::removeOldFile($model, 'picture');
});
2026-10-02 12:09:46 +02:00
static::deleted(function (self $model): void {
2023-08-22 13:38:38 +02:00
FileStorageHelper::removeFile($model, 'picture');
});
}
// * METHODS
/**
* Common methods function.
2023-08-22 13:38:38 +02:00
*/
2026-10-02 12:09:46 +02:00
private static function manageFields(self $model): void
2023-08-22 13:38:38 +02:00
{
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
}
// * RELATIONSHIPS
2022-08-11 10:26:29 +02:00
/**
2026-10-02 12:09:46 +02:00
* Product relation.
2023-08-22 13:38:38 +02:00
*
2026-10-02 12:09:46 +02:00
* @return HasMany<Product, $this>
2022-08-11 10:26:29 +02:00
*/
2023-12-12 12:55:05 +01:00
public function products(): HasMany
{
return $this->hasMany(Product::class);
}
2024-02-15 15:53:36 +01:00
/**
* Activities logs One-to-many relationship.
*
2026-10-02 12:09:46 +02:00
* @return HasMany<ActivityLog, $this>
2024-02-15 15:53:36 +01:00
*/
2026-10-02 12:09:46 +02:00
public function activityLogs(): HasMany
2024-02-15 15:53:36 +01:00
{
return $this->hasMany(ActivityLog::class);
}
}