ldc-doux/app/Models/Moment.php
Sebastien Hattry d3b9865717 delete picture when a field is deleted
+ fix css bugs
2022-01-07 17:29:55 +01:00

55 lines
1.3 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Page;
use App\Lib\Utils;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;
use Kwaadpepper\Enum\Traits\CastsEnums;
class Moment extends Model
{
use HasFactory;
use CastsEnums;
public $enumCasts = [
'page' => Page::class
];
protected $fillable = [
'title',
'subtitle',
'text',
'picture',
'alt_text_picture',
];
protected static function boot()
{
static::creating(function (Moment $moment) {
if ($moment->picture) {
$moment->picture = Utils::storeImage($moment->picture, $moment->getTable());
}
});
static::updating(function (Moment $moment) {
if ($moment->picture) {
$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);
}
}
}
});
parent::boot();
}
}