ldc-gen-game-web/app/Models/Prize.php

101 lines
3 KiB
PHP

<?php
namespace App\Models;
use App\Lib\Helpers\FileStorageHelper;
use Illuminate\Database\Eloquent\Model;
/**
* Prize.
*
* @property string $label_one Singular label
* @property string $label_many Plural label
* @property string $arg_title Description title
* @property string $arg_subtitle Description subtitle
* @property string $arg_content Description content
* @property integer $quantity Quantity
* @property string $pdf_winning_proof Path of the winning proof file
* @property boolean $need_proof Does the prize need a winning proof ?
* @property-read \Illuminate\Support\Carbon $created_at Creation Date.
* @property-read \Illuminate\Support\Carbon $updated_at Last update Date.
*
* @method protected static function booted() The "booted" method of the model.
* @method private static function manageFields($model) Manage model fields.
*
* @property-read \App\Models\GainView[] $gainView Associated gainViews.
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\InstantWin[] $instantWins
* Associated instantWins.
*/
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);
}
/**
* BelongsTo GainView relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function gainView()
{
return $this->belongsTo(GainView::class, 'gain_view_id');
}
/**
* HasMany InstantWin relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function instantWins()
{
return $this->hasMany(InstantWin::class, 'prize_id');
}
}