ldc-legaulois/app/Models/SellItem.php

105 lines
2.8 KiB
PHP
Raw Permalink Normal View History

2024-09-24 16:48:16 +02:00
<?php
namespace App\Models;
use App\Enums\Shop\SellItemsType;
2024-09-24 16:48:16 +02:00
use App\Lib\Helpers\FileStorageHelper;
use App\Traits\Models\ActivityLog;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
2026-09-25 16:25:31 +02:00
* @property int $id Sell item id.
* @property string $name Sell item name.
* @property string $picture Sell item picture path.
* @property string $picture_alt Picture alt attribute.
* @property string $picture_title Picture title attribute.
* @property float $price Sell item price.
* @property int $arcadia_weight Sell item arcadia weight.
* @property int $arcadia_id Sell item arcadia id.
* @property int $order Sell item order.
* @property \App\Enums\Shop\SellItemsType $type Sell item type.
2024-09-24 16:48:16 +02:00
* @property-read \Illuminate\Support\Carbon $created_at Created date.
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
*/
class SellItem extends Model
{
use ActivityLog;
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'type',
2024-09-24 16:48:16 +02:00
'picture',
'picture_alt',
'picture_title',
'price',
'arcadia_id',
'arcadia_weight',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'type' => SellItemsType::class,
];
2024-09-24 16:48:16 +02:00
/**
* Perform any actions required after the model boots.
*/
protected static function booted(): void
{
static::creating(function (self $model) {
self::setOrder($model);
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
});
static::updating(function (self $model) {
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'picture');
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'picture');
});
}
// * METHODS
/**
* Set model's order after the last element of the list.
*/
private static function setOrder(self $mainSlide): void
{
$mainSlide->order = \intval(self::query()->max('order')) + 1;
}
/**
* Getter html_name.
*/
public function getHtmlNameAttribute(): string
{
return nl2br(strip_tags($this->name));
}
/**
* Convert the model instance to an array.
*
2026-09-25 16:25:31 +02:00
* @return array<string,string|int|float|array<string,int>>
2024-09-24 16:48:16 +02:00
*/
public function toArray(): array
{
return array_merge(parent::toArray(), [
2026-09-25 16:25:31 +02:00
'htmlName' => $this->htmlName,
2024-09-24 16:48:16 +02:00
]);
}
}