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

47 lines
1.2 KiB
PHP

<?php
namespace App\Models;
use App\Lib\Utils;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;
class PictoBrand extends Model
{
use HasFactory;
protected $fillable = [
'title',
'picture',
'alt_text_picture',
'text',
];
protected static function boot()
{
static::creating(function (PictoBrand $pictoBrand) {
if ($pictoBrand->picture) {
$pictoBrand->picture = Utils::storeImage($pictoBrand->picture, $pictoBrand->getTable());
}
});
static::updating(function (PictoBrand $pictoBrand) {
if ($pictoBrand->picture) {
$pictoBrand->picture = Utils::storeImage($pictoBrand->picture, $pictoBrand->getTable());
}
});
static::deleted(function (PictoBrand $pictoBrand) {
if ($pictoBrand->picture) {
$urlPicture = public_path() . $pictoBrand->picture;
if ($urlPicture) {
if (File::exists($urlPicture)) {
File::delete($urlPicture);
}
}
}
});
parent::boot();
}
}