ldc-volena/app/Models/Testimonial.php

88 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2023-11-16 09:42:21 +01:00
<?php
namespace App\Models;
2024-03-20 17:39:24 +01:00
use App\Enums\Testimonials\TypeEnum;
2023-11-16 09:42:21 +01:00
use App\Lib\Helpers\FileStorageHelper;
2024-03-05 12:21:04 +01:00
use App\Traits\Models\ActivityLog;
2023-11-16 09:42:21 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @property string $name Name of the person.
* @property string $job Function of the person.
* @property string $content WYSIWYG - Content.
* @property \App\Enums\Testimonials\TypeEnum $type Type of the testimonial.
* @property string $picture Path of the image.
* @property string $picture_alt Alt attribute of the image.
* @property string $picture_title Title attribute of the image.
* @property-read \Carbon\Carbon $created_at Created date.
* @property-read \Carbon\Carbon $updated_at Updated date.
2024-03-05 12:21:04 +01:00
*
* @property-read \App\Models\ActivityLog $activityLogs Activities logs One-to-many relationship.
2023-11-16 09:42:21 +01:00
*/
class Testimonial extends Model
{
2024-03-05 12:21:04 +01:00
use ActivityLog;
/** @use HasFactory<\Database\Factories\TestimonialFactory> */
2023-11-16 09:42:21 +01:00
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var list<string>
2023-11-16 09:42:21 +01:00
*/
protected $fillable = [
'name',
'job',
'content',
'type',
'picture',
'picture_alt',
'picture_title',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
2024-03-20 17:39:24 +01:00
'type' => TypeEnum::class,
2023-11-16 09:42:21 +01:00
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (self $model) {
self::manageFields($model);
});
static::updating(function (self $model) {
self::manageFields($model);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'picture');
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'picture');
});
}
// * METHODS.
2023-11-16 09:42:21 +01:00
/**
* Manage model fields.
*
* @param self $model
* @return void
*/
private static function manageFields(self $model): void
{
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
}
}