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

107 lines
2.9 KiB
PHP
Raw Permalink Normal View History

2021-07-15 16:08:11 +02:00
<?php
namespace App\Models;
2023-08-22 14:25:31 +02:00
use App\Lib\Helpers\FileStorageHelper;
2023-11-30 11:10:02 +01:00
use App\Traits\Models\IsCachedAsCollection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2021-07-15 16:08:11 +02:00
use Illuminate\Database\Eloquent\Model;
2023-12-12 12:55:05 +01:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
2021-07-15 16:08:11 +02:00
/**
* Product.
*
* @property string $name Name
* @property string $picture Path of the picture
* @property string $picture_alt Alt text for the picture
* @property string $picture_title Title text for the picture
2023-12-12 12:25:45 +01:00
* @property integer $ean_code Ean code
* @property-read \Illuminate\Support\Carbon $created_at Creation Date.
* @property-read \Illuminate\Support\Carbon $updated_at Last update Date.
*
* @property-read \App\Models\Range[] $range Associated range.
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Participation[] $participations
* Associated participations.
*/
2021-07-15 16:08:11 +02:00
class Product extends Model
{
use HasFactory;
2023-11-30 11:10:02 +01:00
use IsCachedAsCollection;
2023-08-22 14:25:31 +02:00
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'picture',
'picture_alt',
'picture_title',
2023-12-12 12:25:45 +01:00
'ean_code',
2023-08-22 14:25:31 +02:00
'range_id',
];
// * EVENTS
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
self::manageFields($model);
});
2023-11-30 11:10:02 +01:00
static::created(fn () => self::removeCached());
2023-08-22 14:25:31 +02:00
static::updating(function (self $model) {
self::manageFields($model);
});
static::updated(function (self $model) {
2023-11-30 11:10:02 +01:00
self::removeCached();
2023-08-22 14:25:31 +02:00
FileStorageHelper::removeOldFile($model, 'picture');
});
static::deleted(function (self $model) {
2023-11-30 11:10:02 +01:00
self::removeCached();
2023-08-22 14:25:31 +02:00
FileStorageHelper::removeFile($model, 'picture');
});
}
// * METHODS
/**
* Common methods function.
2023-08-22 14:25:31 +02:00
*
* @param \Illuminate\Database\Eloquent\Model $model
2023-08-22 14:25:31 +02:00
* @return void
*/
private static function manageFields(Model $model)
{
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
}
// * RELATIONSHIPS
2022-08-11 10:26:29 +02:00
/**
* Range relation
2023-08-22 14:25:31 +02:00
*
2022-08-11 10:26:29 +02:00
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2023-12-12 12:55:05 +01:00
public function range(): BelongsTo
{
return $this->belongsTo(Range::class);
}
/**
* Participations relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2023-12-12 12:55:05 +01:00
public function participations(): HasMany
{
return $this->hasMany(Participation::class);
}
2021-07-15 16:08:11 +02:00
}