*/ protected $fillable = [ 'name', 'sticker', 'sticker_alt', 'sticker_title', 'status' ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'status' => 'bool' ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted() { static::creating(function (self $model) { $model->sticker = FileStorageHelper::storeFile($model, $model->sticker, true); }); static::created(function (self $model) { self::flashStatus($model); }); static::updating(function (self $model) { $model->sticker = FileStorageHelper::storeFile($model, $model->sticker, true); }); static::updated(function (self $model) { FileStorageHelper::removeOldFile($model, 'sticker'); self::flashStatus($model, true); }); static::deleting(function (self $model) { FileStorageHelper::removeFile($model, 'sticker'); }); } // * METHODS /** * Flash publication status * * @param self $model * @param boolean $old * @return void */ public static function flashStatus(self $model, bool $old = false) { if ($old and $model->getOriginal('status') == $model->status) { return; } if ($model->status) { session()->flash('info', trans( ':model est maintenant publié', ['model' => Str::singular(trans('models.classes.' . $model->getTable()))] )); } else { session()->flash('warning', trans( ':model est maintenant dépublié', ['model' => Str::singular(trans('models.classes.' . $model->getTable()))] )); } } // * RELATIONSHIPS /** * Get products for the certification. * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function products() { return $this->belongsToMany(Product::class); } }