ldc-legaulois/app/Models/SellItem.php
2026-09-25 16:25:31 +02:00

104 lines
2.8 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Shop\SellItemsType;
use App\Lib\Helpers\FileStorageHelper;
use App\Traits\Models\ActivityLog;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @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.
* @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',
'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,
];
/**
* 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.
*
* @return array<string,string|int|float|array<string,int>>
*/
public function toArray(): array
{
return array_merge(parent::toArray(), [
'htmlName' => $this->htmlName,
]);
}
}