2024-04-25 16:58:41 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2024-06-27 14:10:09 +02:00
|
|
|
use App\Lib\Helpers\FileStorageHelper;
|
2026-09-25 16:25:31 +02:00
|
|
|
use App\Lib\Helpers\ToolboxHelper;
|
2024-04-25 16:58:41 +02:00
|
|
|
use App\Traits\Models\ActivityLog;
|
2026-09-25 16:25:31 +02:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2024-04-25 16:58:41 +02:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2026-10-14 15:50:46 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2024-09-26 12:18:18 +02:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2024-10-09 14:35:27 +02:00
|
|
|
use Illuminate\Support\Str;
|
2024-04-25 16:58:41 +02:00
|
|
|
|
|
|
|
|
/**
|
2026-09-25 16:25:31 +02:00
|
|
|
* @property string $title Consumption moment title.
|
|
|
|
|
* @property string $subtitle Consumption moment subtitle.
|
|
|
|
|
* @property string $slug Consumption moment slug of the title.
|
|
|
|
|
* @property string $picture Consumption moment picture.
|
|
|
|
|
* @property string $picture_alt Picture alt attribute.
|
|
|
|
|
* @property string $picture_title Picture title attribute.
|
2024-06-27 14:10:09 +02:00
|
|
|
* @property-read \Illuminate\Support\Carbon $created_at Created date.
|
|
|
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
|
2024-04-25 16:58:41 +02:00
|
|
|
*/
|
|
|
|
|
class ConsumptionMoment extends Model
|
|
|
|
|
{
|
|
|
|
|
use ActivityLog;
|
|
|
|
|
use HasFactory;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
*
|
|
|
|
|
* @var array<int, string>
|
|
|
|
|
*/
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'title',
|
2024-06-27 14:10:09 +02:00
|
|
|
'subtitle',
|
2024-09-18 13:08:34 +02:00
|
|
|
'description',
|
2024-05-22 12:02:14 +02:00
|
|
|
'slug',
|
2024-06-27 14:10:09 +02:00
|
|
|
'picture',
|
|
|
|
|
'picture_alt',
|
|
|
|
|
'picture_title',
|
2024-04-25 16:58:41 +02:00
|
|
|
];
|
2024-06-24 14:31:32 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Perform any actions required after the model boots.
|
|
|
|
|
*/
|
|
|
|
|
protected static function booted(): void
|
|
|
|
|
{
|
|
|
|
|
static::creating(function (self $model) {
|
|
|
|
|
self::assertTitleIsUnique($model->title);
|
2024-10-09 14:35:27 +02:00
|
|
|
$model->slug = Str::slug($model->title);
|
2024-06-27 14:10:09 +02:00
|
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
2024-06-24 14:31:32 +02:00
|
|
|
});
|
2024-09-27 10:26:15 +02:00
|
|
|
static::created(function () {
|
2024-09-26 12:18:18 +02:00
|
|
|
self::resetCache();
|
|
|
|
|
});
|
2024-06-24 14:31:32 +02:00
|
|
|
static::updating(function (self $model) {
|
|
|
|
|
self::assertTitleIsUnique($model->title, $model->getKey());
|
2024-10-09 14:35:27 +02:00
|
|
|
$model->slug = Str::slug($model->title);
|
2024-06-27 14:10:09 +02:00
|
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
|
|
|
});
|
|
|
|
|
static::updated(function (self $model) {
|
|
|
|
|
FileStorageHelper::removeOldFile($model, 'picture');
|
2024-09-26 12:18:18 +02:00
|
|
|
self::resetCache();
|
2024-06-27 14:10:09 +02:00
|
|
|
});
|
|
|
|
|
static::deleted(function (self $model) {
|
|
|
|
|
FileStorageHelper::removeFile($model, 'picture');
|
2024-09-26 12:18:18 +02:00
|
|
|
self::resetCache();
|
2024-06-24 14:31:32 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// * METHODS
|
|
|
|
|
|
|
|
|
|
/**
|
2024-09-26 17:39:57 +02:00
|
|
|
* Asserts using validation that the title is unique.
|
2024-06-24 14:31:32 +02:00
|
|
|
*
|
|
|
|
|
* @throws \Illuminate\Validation\ValidationException If an actu title already exists.
|
|
|
|
|
*/
|
2026-09-25 16:25:31 +02:00
|
|
|
private static function assertTitleIsUnique(?string $title, ?int $id = null): void
|
2024-06-24 14:31:32 +02:00
|
|
|
{
|
|
|
|
|
$table = (new self())->getTable();
|
|
|
|
|
ToolboxHelper::assertFieldIsUnique($table, 'title', $title, $id);
|
2024-10-09 14:35:27 +02:00
|
|
|
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($title), $id);
|
2024-06-24 14:31:32 +02:00
|
|
|
}
|
2024-09-26 12:18:18 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reset all cache elements related to this model.
|
|
|
|
|
*/
|
2024-10-04 17:03:04 +02:00
|
|
|
private static function resetCache(): void
|
2024-09-26 12:18:18 +02:00
|
|
|
{
|
|
|
|
|
Cache::forget('consumptionMomentsOrderedBySlug');
|
|
|
|
|
self::getOrderedBySlug();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all consumption moments ordered by slug.
|
|
|
|
|
*/
|
2026-09-25 16:25:31 +02:00
|
|
|
public static function getOrderedBySlug(): Collection
|
2024-09-26 12:18:18 +02:00
|
|
|
{
|
2025-04-01 12:53:37 +02:00
|
|
|
return Cache::remember('consumptionMomentsOrderedBySlug', 86400, function () {
|
2024-09-26 12:18:18 +02:00
|
|
|
return self::query()->orderBy('slug')->get();
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-10-14 15:50:46 +02:00
|
|
|
|
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all of the recipes for the consumption moment.
|
|
|
|
|
*/
|
|
|
|
|
public function recipes(): BelongsToMany
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Recipe::class);
|
|
|
|
|
}
|
2024-04-25 16:58:41 +02:00
|
|
|
}
|