111 lines
3.3 KiB
PHP
111 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Traits\Models\IsCachedAsCollection;
|
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
|
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 \Illuminate\Database\Eloquent\Collection|\App\Models\InstantWin[] $instantWins
|
|
* Associated instantWins.
|
|
*/
|
|
class Prize extends Model
|
|
{
|
|
use IsCachedAsCollection;
|
|
|
|
/**
|
|
* 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'
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
static::creating(function (self $model) {
|
|
self::manageFields($model);
|
|
});
|
|
static::created(function () {
|
|
self::removeCached();
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::manageFields($model);
|
|
});
|
|
static::updated(function (self $model) {
|
|
self::removeCached();
|
|
FileStorageHelper::removeOldFile($model, 'pdf_winning_proof');
|
|
});
|
|
static::deleted(function (self $model) {
|
|
self::removeCached();
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* HasMany InstantWin relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function instantWins()
|
|
{
|
|
return $this->hasMany(InstantWin::class, 'prize_id');
|
|
}
|
|
|
|
// * CACHE METHOD
|
|
|
|
/**
|
|
* Change query for cached elements
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
*/
|
|
protected static function queryCached(Builder $query): Builder
|
|
{
|
|
return $query->with('instantWins');
|
|
}
|
|
}
|