ldc-volena/app/Models/Logo.php
2023-11-16 09:42:21 +01:00

80 lines
2 KiB
PHP

<?php
namespace App\Models;
use App\Lib\Helpers\FileStorageHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @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.
*
* @property-read \App\Models\Site $sites Sites Many-to-many relationship.
*/
class Logo extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'picture',
'picture_alt',
'picture_title',
];
/**
* 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
/**
* Manage model fields.
*
* @param self $model
* @return void
*/
private static function manageFields(self $model): void
{
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
}
// * RELATIONS
/**
* The sites that belong to the logo.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function sites(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(Site::class);
}
}