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

121 lines
3.3 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;
2024-02-15 15:53:36 +01:00
use App\Traits\Models\ActivityLog;
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.
2024-02-15 15:53:36 +01:00
*
* @property-read \App\Models\ActivityLog $activityLogs Activities logs One-to-many relationship.
*/
2021-07-15 16:08:11 +02:00
class Product extends Model
{
2024-02-15 15:53:36 +01:00
use ActivityLog;
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.
*
2024-01-31 09:03:18 +00:00
* @var array<string>
2023-08-22 14:25:31 +02:00
*/
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
*
2024-01-31 09:03:18 +00:00
* @param self $model
2023-08-22 14:25:31 +02:00
* @return void
*/
2024-01-31 09:03:18 +00:00
private static function manageFields(self $model)
2023-08-22 14:25:31 +02:00
{
$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);
}
2024-02-15 15:53:36 +01:00
/**
* Activities logs One-to-many relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function activityLogs(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(ActivityLog::class);
}
2021-07-15 16:08:11 +02:00
}