ldc-legaulois/app/Models/Approach.php
2024-07-01 14:07:43 +02:00

84 lines
2.6 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Approaches\ApproachTypeEnum;
use App\Lib\Helpers\FileStorageHelper;
use App\Traits\Models\ActivityLog;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @property \App\Enums\Approaches\ApproachTypeEnum $type Approach type (label or demarche).
* @property string $name Approach name.
* @property string $picture Approach picture.
* @property string $picture_alt Picture alt attribute.
* @property string $picture_title Picture title attribute.
* @property string $button_url Approach button url.
* @property boolean $published Approach publication status.
* @property-read \Carbon\Carbon $created_at Created date.
* @property-read \Carbon\Carbon $updated_at Updated date.
*/
class Approach extends Model
{
use ActivityLog;
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'type',
'name',
'picture',
'picture_alt',
'picture_title',
'button_url',
'published',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'type' => ApproachTypeEnum::class,
'published' => 'boolean'
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (self $approach) {
$approach->picture = FileStorageHelper::storeFile($approach, $approach->picture, true);
});
static::updating(function (self $approach) {
$approach->picture = FileStorageHelper::storeFile($approach, $approach->picture, true);
});
static::updated(function (self $approach) {
FileStorageHelper::removeOldFile($approach, 'picture');
});
static::deleted(function (self $approach) {
FileStorageHelper::removeFile($approach, 'picture');
});
}
// * RELATIONSHIPS
/**
* Get MainSlides of the Approach (morph-to-many relationship).
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function mainSlides(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(MainSlide::class);
}
}