ldc-doux/app/Models/PictoBrand.php

73 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2021-12-13 16:55:12 +01:00
<?php
namespace App\Models;
2021-12-15 10:09:39 +01:00
use App\Lib\Utils;
2021-12-13 16:55:12 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;
2022-01-23 11:45:06 +01:00
use Spatie\Translatable\HasTranslations;
2021-12-13 16:55:12 +01:00
class PictoBrand extends Model
{
use HasFactory;
2022-01-23 11:45:06 +01:00
use HasTranslations;
2021-12-13 16:55:12 +01:00
2022-08-29 17:23:10 +02:00
/**
* The attributes that are mass assignable.
*
* @var array
*/
2026-10-27 09:12:51 +01:00
#[\Override]
2021-12-13 16:55:12 +01:00
protected $fillable = [
'title',
'picture',
2021-12-20 14:02:37 +01:00
'alt_text_picture',
2021-12-13 16:55:12 +01:00
'text',
];
2022-08-29 17:23:10 +02:00
/**
* The attributes that are translatable.
*
* @var array
*/
2022-01-23 11:45:06 +01:00
public $translatable = [
'title',
'picture',
'alt_text_picture',
'text',
];
2022-08-29 17:23:10 +02:00
/**
* Perform any actions required after the model boots.
*
* @return void
*/
2021-12-15 10:09:39 +01:00
protected static function boot()
{
2026-10-27 09:12:51 +01:00
static::creating(function (PictoBrand $pictoBrand): void {
2022-07-27 14:33:56 +02:00
if (is_a($pictoBrand->picture, "SplFileInfo")) {
2021-12-15 10:09:39 +01:00
$pictoBrand->picture = Utils::storeImage($pictoBrand->picture, $pictoBrand->getTable());
}
});
2021-12-13 16:55:12 +01:00
2026-10-27 09:12:51 +01:00
static::updating(function (PictoBrand $pictoBrand): void {
2022-07-27 14:33:56 +02:00
if (is_a($pictoBrand->picture, "SplFileInfo")) {
2021-12-15 10:09:39 +01:00
$pictoBrand->picture = Utils::storeImage($pictoBrand->picture, $pictoBrand->getTable());
}
});
2026-10-27 09:12:51 +01:00
static::deleted(function (PictoBrand $pictoBrand): void {
if ($pictoBrand->picture) {
$urlPicture = public_path() . $pictoBrand->picture;
if ($urlPicture) {
if (File::exists($urlPicture)) {
File::delete($urlPicture);
}
}
}
});
2021-12-15 10:09:39 +01:00
parent::boot();
}
2021-12-13 16:55:12 +01:00
}