112 lines
3.2 KiB
PHP
112 lines
3.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;
|
|
use Spatie\Translatable\HasTranslations;
|
|
|
|
class Advice extends Model
|
|
{
|
|
use HasFactory;
|
|
use HasTranslations;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'title1',
|
|
'title2',
|
|
'picture1',
|
|
'alt_text_picture1',
|
|
'picture2',
|
|
'alt_text_picture2',
|
|
'picture3',
|
|
'alt_text_picture3',
|
|
'text1',
|
|
'text2',
|
|
];
|
|
|
|
/**
|
|
* The attributes that are translatable.
|
|
*
|
|
* @var array
|
|
*/
|
|
public $translatable = [
|
|
'title1',
|
|
'title2',
|
|
'picture1',
|
|
'alt_text_picture1',
|
|
'picture2',
|
|
'alt_text_picture2',
|
|
'picture3',
|
|
'alt_text_picture3',
|
|
'text1',
|
|
'text2',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
|
|
*/
|
|
protected static function boot()
|
|
{
|
|
static::creating(function (Advice $advice) {
|
|
if (is_a($advice->picture1, "SplFileInfo")) {
|
|
$advice->picture1 = Utils::storeImage($advice->picture1, $advice->getTable());
|
|
}
|
|
if (is_a($advice->picture2, "SplFileInfo")) {
|
|
$advice->picture2 = Utils::storeImage($advice->picture2, $advice->getTable());
|
|
}
|
|
if (is_a($advice->picture3, "SplFileInfo")) {
|
|
$advice->picture3 = Utils::storeImage($advice->picture3, $advice->getTable());
|
|
}
|
|
});
|
|
|
|
static::updating(function (Advice $advice) {
|
|
if (is_a($advice->picture1, "SplFileInfo")) {
|
|
$advice->picture1 = Utils::storeImage($advice->picture1, $advice->getTable());
|
|
}
|
|
if (is_a($advice->picture2, "SplFileInfo")) {
|
|
$advice->picture2 = Utils::storeImage($advice->picture2, $advice->getTable());
|
|
}
|
|
if (is_a($advice->picture3, "SplFileInfo")) {
|
|
$advice->picture3 = Utils::storeImage($advice->picture3, $advice->getTable());
|
|
}
|
|
});
|
|
|
|
static::deleted(function (Advice $advice) {
|
|
if ($advice->picture1) {
|
|
$urlPicture = public_path() . $advice->picture1;
|
|
if ($urlPicture) {
|
|
if (File::exists($urlPicture)) {
|
|
File::delete($urlPicture);
|
|
}
|
|
}
|
|
}
|
|
if ($advice->picture2) {
|
|
$urlPicture = public_path() . $advice->picture2;
|
|
if ($urlPicture) {
|
|
if (File::exists($urlPicture)) {
|
|
File::delete($urlPicture);
|
|
}
|
|
}
|
|
}
|
|
if ($advice->picture3) {
|
|
$urlPicture = public_path() . $advice->picture3;
|
|
if ($urlPicture) {
|
|
if (File::exists($urlPicture)) {
|
|
File::delete($urlPicture);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
parent::boot();
|
|
}
|
|
}
|