ldc-doux/app/Models/Post.php
2021-12-20 14:02:37 +01:00

56 lines
1.4 KiB
PHP

<?php
namespace App\Models;
use App\Lib\Utils;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'date_start' => 'datetime',
'date_end' => 'datetime',
];
protected $fillable = [
'title',
'text',
'picture_home',
'alt_text_picture_home',
'picture_news',
'alt_text_picture_news',
'date_start',
'date_end',
'on_top'
];
protected static function boot()
{
static::creating(function (Post $post) {
if ($post->picture_home) {
$post->picture_home = Utils::storeImage($post->picture_home, $post->getTable());
}
if ($post->picture_news) {
$post->picture_news = Utils::storeImage($post->picture_news, $post->getTable());
}
});
static::updating(function (Post $post) {
if ($post->picture_home) {
$post->picture_home = Utils::storeImage($post->picture_home, $post->getTable());
}
if ($post->picture_news) {
$post->picture_news = Utils::storeImage($post->picture_news, $post->getTable());
}
});
parent::boot();
}
}