2020-11-05 17:08:32 +01:00
|
|
|
<?php
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2020-11-05 17:08:32 +01:00
|
|
|
namespace App\Models;
|
|
|
|
|
|
2026-09-29 14:51:46 +02:00
|
|
|
use App\Interfaces\ModelWithOrder;
|
|
|
|
|
use App\Interfaces\ModelWithStatus;
|
2026-06-14 21:32:07 +02:00
|
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
|
|
|
use App\Lib\Helpers\ToolboxHelper;
|
2026-09-29 14:51:46 +02:00
|
|
|
use App\Traits\Models\HasOrder;
|
|
|
|
|
use App\Traits\Models\HasStatus;
|
2026-06-14 21:32:07 +02:00
|
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
2020-11-05 17:08:32 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2026-06-14 21:32:07 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
use Uwil\Laravel\Redirections\Interfaces\RedirectableInterface;
|
|
|
|
|
use Uwil\Laravel\Redirections\Traits\IsRedirectable;
|
2020-11-05 17:08:32 +01:00
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
/**
|
|
|
|
|
* @property int $id
|
|
|
|
|
* @property string $name
|
|
|
|
|
* @property string $slug
|
|
|
|
|
* @property string|null $description
|
|
|
|
|
* @property string|null $picture
|
|
|
|
|
* @property string $picture_alt
|
|
|
|
|
* @property string $picture_title
|
|
|
|
|
* @property bool $status
|
|
|
|
|
* @property int $order
|
|
|
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
|
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Product> $products
|
|
|
|
|
*
|
|
|
|
|
* @mixin \Eloquent
|
|
|
|
|
*/
|
|
|
|
|
#[Fillable([
|
|
|
|
|
'name',
|
|
|
|
|
'description',
|
|
|
|
|
'picture',
|
|
|
|
|
'picture_alt',
|
|
|
|
|
'picture_title',
|
|
|
|
|
'status',
|
|
|
|
|
])]
|
2026-09-29 14:51:46 +02:00
|
|
|
class Range extends Model implements ModelWithOrder, ModelWithStatus, RedirectableInterface
|
2020-11-05 17:08:32 +01:00
|
|
|
{
|
2026-09-29 14:51:46 +02:00
|
|
|
use HasOrder;
|
|
|
|
|
use HasStatus;
|
2026-06-14 21:32:07 +02:00
|
|
|
use IsRedirectable;
|
|
|
|
|
|
|
|
|
|
#[\Override]
|
|
|
|
|
protected static function booted()
|
|
|
|
|
{
|
|
|
|
|
static::creating(function (self $model): void {
|
|
|
|
|
self::manageFields($model);
|
|
|
|
|
});
|
|
|
|
|
static::updating(function (self $model): void {
|
|
|
|
|
self::manageFields($model);
|
|
|
|
|
});
|
|
|
|
|
static::deleting(function (self $model): void {
|
|
|
|
|
FileStorageHelper::removeFile($model, 'picture');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[\Override]
|
|
|
|
|
public function getRouteKeyName(): string
|
|
|
|
|
{
|
|
|
|
|
return 'slug';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return string[]|string */
|
|
|
|
|
#[\Override]
|
|
|
|
|
public function getFrontUrl(string $modelKey): array | string
|
|
|
|
|
{
|
|
|
|
|
return route('fo.products.index') . '#' . http_build_query([
|
|
|
|
|
'ranges' => [$modelKey],
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function manageFields(self $model): void
|
|
|
|
|
{
|
|
|
|
|
self::assertNameIsUnique($model->name, $model->id);
|
|
|
|
|
$model->slug = Str::slug($model->name);
|
|
|
|
|
|
|
|
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
|
|
|
if (is_null($model->order)) {
|
2026-09-29 16:59:07 +02:00
|
|
|
$maxOrder = self::query()->max('order');
|
|
|
|
|
$model->order = is_numeric($maxOrder) ? (int) $maxOrder + 1 : 0;
|
2026-06-14 21:32:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function assertNameIsUnique(?string $name, ?int $id = null): void
|
|
|
|
|
{
|
|
|
|
|
$table = new self()->getTable();
|
|
|
|
|
ToolboxHelper::assertFieldIsUnique($table, 'name', $name, $id);
|
|
|
|
|
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($name ?? ''), $id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return \Illuminate\Database\Eloquent\Relations\HasMany<\App\Models\Product,$this> */
|
|
|
|
|
public function products(): HasMany
|
2022-09-08 11:29:39 +02:00
|
|
|
{
|
2026-06-14 21:32:07 +02:00
|
|
|
return $this->hasMany(Product::class, 'range_id');
|
2020-11-05 17:08:32 +01:00
|
|
|
}
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
#[\Override]
|
|
|
|
|
protected function casts(): array
|
2022-09-08 11:29:39 +02:00
|
|
|
{
|
2026-06-14 21:32:07 +02:00
|
|
|
return [
|
|
|
|
|
'status' => 'bool',
|
|
|
|
|
];
|
2020-11-05 17:08:32 +01:00
|
|
|
}
|
|
|
|
|
}
|