ldc-legaulois/app/Models/HistoricalMilestone.php
2024-07-15 10:34:42 +02:00

79 lines
2.5 KiB
PHP

<?php
namespace App\Models;
use App\Lib\Helpers\FileStorageHelper;
use App\Traits\Models\ActivityLog;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @property \Carbon\Carbon $year Historical milestone year.
* @property string $title Historical milestone title.
* @property string $description Historical milestone description.
* @property string $picture Historical milestone picture.
* @property string $picture_alt Picture alt attribute.
* @property string $picture_title Picture title attribute.
* @property boolean $published Historical milestone publish status.
* @property-read \Illuminate\Support\Carbon $created_at Created date.
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
*/
class HistoricalMilestone extends Model
{
use ActivityLog;
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'year',
'title',
'description',
'picture',
'picture_alt',
'picture_title',
'published',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'year' => 'integer',
'published' => 'boolean',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (self $historicalMilestone) {
$historicalMilestone->picture = FileStorageHelper::storeFile(
$historicalMilestone,
$historicalMilestone->picture,
true
);
});
static::updating(function (self $historicalMilestone) {
$historicalMilestone->picture = FileStorageHelper::storeFile(
$historicalMilestone,
$historicalMilestone->picture,
true
);
});
static::updated(function (self $historicalMilestone) {
FileStorageHelper::removeOldFile($historicalMilestone, 'picture');
});
static::deleted(function (self $historicalMilestone) {
FileStorageHelper::removeFile($historicalMilestone, 'picture');
});
}
}