ldc-doux/app/Models/Moment.php

64 lines
1.5 KiB
PHP
Raw Normal View History

2021-12-15 10:09:39 +01:00
<?php
namespace App\Models;
use App\Enums\Page;
use App\Lib\Utils;
2022-01-30 13:57:46 +01:00
use App\Traits\CastsEnumsHasTranslations;
2021-12-15 10:09:39 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;
2021-12-15 10:09:39 +01:00
class Moment extends Model
{
use HasFactory;
2022-01-30 13:57:46 +01:00
use CastsEnumsHasTranslations;
2021-12-15 10:09:39 +01:00
public $enumCasts = [
'page' => Page::class
];
protected $fillable = [
'title',
'subtitle',
'text',
'picture',
2021-12-20 14:02:37 +01:00
'alt_text_picture',
2021-12-15 10:09:39 +01:00
];
2022-01-30 13:57:46 +01:00
public $translatable = [
'title',
'subtitle',
'text',
'picture',
'alt_text_picture',
];
2021-12-15 10:09:39 +01:00
protected static function boot()
{
static::creating(function (Moment $moment) {
2022-01-30 13:57:46 +01:00
if (is_a($moment->picture, "SplFileInfo")) {
2021-12-15 10:09:39 +01:00
$moment->picture = Utils::storeImage($moment->picture, $moment->getTable());
}
});
static::updating(function (Moment $moment) {
2022-01-30 13:57:46 +01:00
if (is_a($moment->picture, "SplFileInfo")) {
2021-12-15 10:09:39 +01:00
$moment->picture = Utils::storeImage($moment->picture, $moment->getTable());
}
});
static::deleted(function (Moment $moment) {
if ($moment->picture) {
$urlPicture = public_path() . $moment->picture;
if ($urlPicture) {
if (File::exists($urlPicture)) {
File::delete($urlPicture);
}
}
}
});
2021-12-15 10:09:39 +01:00
parent::boot();
}
}