145 lines
4.3 KiB
PHP
145 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Traits\Models\ActivityLog;
|
|
use App\Traits\Models\HasTranslations;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
|
|
use Illuminate\Support\Str;
|
|
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
|
|
use Uwil\LaravelCommon\Helpers\FileHelper;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use Illuminate\Http\UploadedFile;
|
|
use App\Helpers\ShowroomFileHelper;
|
|
|
|
/**
|
|
* @property string $name Name of the product.
|
|
* @property string $slug Slug of the name for the product.
|
|
* @property string $description WYSIWYG text description using ckeditor.
|
|
* @property string $picture Picture path for the product.
|
|
* @property boolean $status Visible ? (présent dans l'export).
|
|
* @property string $tenant_id Id of the related tenant.
|
|
* @property-read \Illuminate\Support\Carbon $created_at Created date.
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
|
|
*
|
|
* @property-read \App\Models\Range $range Range Belongs-to.
|
|
*/
|
|
class Product extends Model
|
|
{
|
|
use HasFactory;
|
|
use HasTranslations;
|
|
use BelongsToTenant;
|
|
use ActivityLog;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'description',
|
|
'picture',
|
|
'status',
|
|
'range_id',
|
|
];
|
|
|
|
/**
|
|
* Translatable fields array
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
public $translatable = [
|
|
'name',
|
|
'slug',
|
|
'description',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'status' => 'bool',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
// * ASSERTIONS
|
|
self::assertNameIsUnique($model->name);
|
|
|
|
// * TRANSFORMATIONS
|
|
$model->slug = Str::slug($model->name);
|
|
/** @phpstan-ignore-next-line */
|
|
if ($model->picture instanceof UploadedFile) {
|
|
$model->picture = ShowroomFileHelper::storeFileWithUUIDName($model->picture, $model);
|
|
}
|
|
});
|
|
static::updating(function (self $model) {
|
|
// * ASSERTIONS
|
|
self::assertNameIsUnique($model->name, $model->id);
|
|
|
|
// * TRANSFORMATIONS
|
|
$model->slug = Str::slug($model->name);
|
|
/** @phpstan-ignore-next-line */
|
|
if ($model->picture instanceof UploadedFile) {
|
|
$model->picture = ShowroomFileHelper::storeFileWithUUIDName($model->picture, $model);
|
|
}
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileHelper::removeOldFile($model, 'picture');
|
|
});
|
|
static::deleted(function (self $model) {
|
|
FileHelper::removeFile($model, 'picture');
|
|
});
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* Get the range that owns the Product
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function range(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Range::class);
|
|
}
|
|
|
|
/**
|
|
* Get the brand that owns the Product
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOneThrough
|
|
*/
|
|
public function brand(): HasOneThrough
|
|
{
|
|
return $this->hasOneThrough(Brand::class, Range::class, 'id', 'id', 'range_id', 'brand_id');
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Asserts using validation that the name is unique
|
|
*
|
|
* @param string|null $name
|
|
* @param integer|null $id
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException If name already exists.
|
|
*/
|
|
private static function assertNameIsUnique(string|null $name, ?int $id = null)
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, 'name', $name, $id);
|
|
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($name), $id);
|
|
}
|
|
}
|