ldc-supportweb/app/Models/Brand.php
2026-06-05 17:05:46 +02:00

106 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Uwil\LaravelActivityLogs\Traits\HasActivityLog;
use Uwil\LaravelBackend\Traits\Models\HasCache;
use Uwil\LaravelBackend\Traits\Models\HasModelNaming;
use Uwil\LaravelI18n\Helpers\TranslationHelper;
use Uwil\LaravelI18n\Traits\Models\HasTranslations;
/**
* @property int $id Primary key of the brand.
* @property string $name Name of the brand.
* @property-read \Illuminate\Support\Carbon $created_at Date the brand was created.
* @property-read \Illuminate\Support\Carbon $updated_at Date the brand was updated.
* @property-read \Illuminate\Support\Carbon $deleted_at Date the brand was deleted.
*/
class Brand extends Model
{
use HasActivityLog;
use HasCache;
/** @use \Illuminate\Database\Eloquent\Factories\HasFactory<\Database\Factories\BrandFactory> */
use HasFactory;
use HasModelNaming;
use HasTranslations;
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
#[\Override]
protected $fillable = [
'name',
];
/**
* The translatable columns.
*
* @var list<string>
*/
public $translatable = [
'name',
];
/**
* Perform any actions required after the model boots.
*/
#[\Override]
protected static function booted(): void
{
static::creating(function (self $model): void {
self::manageFields($model);
});
static::updating(function (self $model): void {
self::manageFields($model);
});
}
// * METHODS.
/**
* Get relations to include in the cache.
*
* @return list<string>
*/
public static function getCacheRelations(): array
{
return ['sites'];
}
/**
* Manage model fields.
*/
private static function manageFields(self $model): void
{
TranslationHelper::assertTranslationFieldIsUnique($model, $model->name, 'name');
}
// * RELATIONS.
/**
* Get sites associated to the brand.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany<\App\Models\Site,$this,\Illuminate\Database\Eloquent\Relations\MorphPivot,'pivot'>
*/
public function sites(): MorphToMany
{
return $this->morphToMany(
Site::class,
'sitable',
'sitables',
'sitable_id',
'site_id',
)->withPivot('created_at');
}
}