ldc-gen-game-web/app/Models/Prize.php
2023-08-22 12:57:02 +02:00

70 lines
1.6 KiB
PHP

<?php
namespace App\Models;
use App\Lib\Helpers\FileStorageHelper;
use Illuminate\Database\Eloquent\Model;
class Prize extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'label_one',
'label_many',
'arg_title',
'arg_subtitle',
'arg_content',
'quantity',
'pdf_winning_proof',
'need_proof',
'gain_view_id',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
self::manageFields($model);
});
static::updating(function (self $model) {
self::manageFields($model);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'pdf_winning_proof');
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'pdf_winning_proof');
});
}
// * METHODS
/**
* Common methods function.
*
* @param Model $model
* @return void
*/
private static function manageFields(Model $model)
{
$model->pdf_winning_proof = FileStorageHelper::storeFile($model, $model->pdf_winning_proof, true);
}
public function gain_view()
{
return $this->belongsTo(GainView::class, 'gain_view_id');
}
public function instant_wins()
{
return $this->hasMany(InstantWin::class, 'prize_id');
}
}