ldc-doux/app/Models/Gamme.php
2021-12-20 17:21:09 +01:00

55 lines
1.3 KiB
PHP

<?php
namespace App\Models;
use App\Lib\Utils;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Gamme extends Model
{
use HasFactory;
protected $fillable = [
'parent_id',
'title',
'text',
'color',
'picture',
'alt_text_picture',
'banner',
'alt_text_banner',
];
protected static function boot()
{
static::creating(function (Gamme $gamme) {
if ($gamme->picture) {
$gamme->picture = Utils::storeImage($gamme->picture, $gamme->getTable());
}
if ($gamme->banner) {
$gamme->banner = Utils::storeImage($gamme->banner, $gamme->getTable());
}
});
static::updating(function (Gamme $gamme) {
if ($gamme->picture) {
$gamme->picture = Utils::storeImage($gamme->picture, $gamme->getTable());
}
if ($gamme->banner) {
$gamme->banner = Utils::storeImage($gamme->banner, $gamme->getTable());
}
});
parent::boot();
}
public function childGammes()
{
return $this->hasMany(Gamme::class, 'parent_gamme_id');
}
public function parentGamme()
{
return $this->belongsTo(Gamme::class);
}
}