79 lines
1.8 KiB
PHP
79 lines
1.8 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 Slide extends Model
|
|
{
|
|
use HasFactory;
|
|
use HasTranslations;
|
|
|
|
/**
|
|
* 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',
|
|
'picture',
|
|
'alt_text_picture',
|
|
'date_start',
|
|
'date_end'
|
|
];
|
|
|
|
public $translatable = [
|
|
'title',
|
|
'text',
|
|
'picture',
|
|
'link',
|
|
'link_label',
|
|
'alt_text_picture'
|
|
];
|
|
|
|
protected static function boot()
|
|
{
|
|
static::creating(function (Slide $slide) {
|
|
if (is_a($slide->picture,"SplFileInfo")) {
|
|
$slide->picture = Utils::storeImage($slide->picture, $slide->getTable());
|
|
}
|
|
});
|
|
|
|
static::updating(function (Slide $slide) {
|
|
if (is_a($slide->picture,"SplFileInfo")) {
|
|
$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);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
parent::boot();
|
|
}
|
|
|
|
public function slider()
|
|
{
|
|
return $this->belongsTo(Slider::class);
|
|
}
|
|
}
|