ldc-doux/app/Models/Slide.php

80 lines
1.8 KiB
PHP
Raw 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
*/
protected $casts = [
'date_start' => 'datetime',
'date_end' => 'datetime',
];
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'
];
public $translatable = [
'title',
'text',
2022-01-21 14:26:44 +01:00
'picture',
'link',
'link_label',
'alt_text_picture'
];
2021-12-07 23:35:14 +01:00
protected static function boot()
{
static::creating(function (Slide $slide) {
2022-01-21 14:26:44 +01:00
if (is_a($slide->picture,"SplFileInfo")) {
2021-12-07 23:35:14 +01:00
$slide->picture = Utils::storeImage($slide->picture, $slide->getTable());
}
});
static::updating(function (Slide $slide) {
2022-01-21 14:26:44 +01:00
if (is_a($slide->picture,"SplFileInfo")) {
2021-12-07 23:35:14 +01:00
$slide->picture = Utils::storeImage($slide->picture, $slide->getTable());
}
});
static::deleted(function (Slide $slide) {
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();
}
2021-12-06 08:51:51 +01:00
public function slider()
{
return $this->belongsTo(Slider::class);
}
}