ldc-doux/app/Models/Slide.php

102 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2021-12-06 08:51:51 +01:00
<?php
namespace App\Models;
2021-12-07 23:35:14 +01:00
use App\Lib\Utils;
2021-12-06 08:51:51 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;
use Spatie\Translatable\HasTranslations;
2021-12-06 08:51:51 +01:00
class Slide extends Model
{
use HasFactory;
use HasTranslations;
2021-12-06 08:51:51 +01:00
/**
* The attributes that should be cast to native types.
*
* @var array
*/
2026-10-27 09:12:51 +01:00
#[\Override]
2021-12-06 08:51:51 +01:00
protected $casts = [
'date_start' => 'datetime',
'date_end' => 'datetime',
];
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-06 08:51:51 +01:00
protected $fillable = [
'title',
'text',
'link_label',
'link',
'link_new_tab',
'slider_id',
2021-12-07 23:35:14 +01:00
'picture',
2021-12-17 17:27:25 +01:00
'alt_text_picture',
2021-12-06 08:51:51 +01:00
'date_start',
'date_end'
];
2022-08-29 17:23:10 +02:00
/**
* The attributes that are translatable.
*
* @var array
*/
public $translatable = [
'title',
'text',
2022-01-21 14:26:44 +01:00
'picture',
'link',
'link_label',
'alt_text_picture'
];
2022-08-29 17:23:10 +02:00
/**
* Perform any actions required after the model boots.
*
* @return void
*/
2021-12-07 23:35:14 +01:00
protected static function boot()
{
2026-10-27 09:12:51 +01:00
static::creating(function (Slide $slide): void {
2022-07-27 14:33:56 +02:00
if (is_a($slide->picture, "SplFileInfo")) {
2021-12-07 23:35:14 +01:00
$slide->picture = Utils::storeImage($slide->picture, $slide->getTable());
}
});
2026-10-27 09:12:51 +01:00
static::updating(function (Slide $slide): void {
2022-07-27 14:33:56 +02:00
if (is_a($slide->picture, "SplFileInfo")) {
2021-12-07 23:35:14 +01:00
$slide->picture = Utils::storeImage($slide->picture, $slide->getTable());
}
});
2026-10-27 09:12:51 +01:00
static::deleted(function (Slide $slide): void {
if ($slide->picture) {
$urlPicture = public_path() . $slide->picture;
if ($urlPicture) {
if (File::exists($urlPicture)) {
File::delete($urlPicture);
}
}
}
});
2021-12-07 23:35:14 +01:00
parent::boot();
}
2022-08-29 17:23:10 +02:00
/**
* BelongsTo slider relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2021-12-06 08:51:51 +01:00
public function slider()
{
return $this->belongsTo(Slider::class);
}
}