ldc-doux/app/Models/PictoBrand.php
2022-01-23 11:45:06 +01:00

56 lines
1.5 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;
use Spatie\Translatable\HasTranslations;
class PictoBrand extends Model
{
use HasFactory;
use HasTranslations;
protected $fillable = [
'title',
'picture',
'alt_text_picture',
'text',
];
public $translatable = [
'title',
'picture',
'alt_text_picture',
'text',
];
protected static function boot()
{
static::creating(function (PictoBrand $pictoBrand) {
if (is_a($pictoBrand->picture,"SplFileInfo")) {
$pictoBrand->picture = Utils::storeImage($pictoBrand->picture, $pictoBrand->getTable());
}
});
static::updating(function (PictoBrand $pictoBrand) {
if (is_a($pictoBrand->picture,"SplFileInfo")) {
$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();
}
}