ldc-supportweb/app/Models/Brand.php

107 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2026-05-19 16:44:38 +02:00
<?php
declare(strict_types=1);
2026-05-19 16:44:38 +02:00
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
2026-05-19 16:44:38 +02:00
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;
2026-05-19 16:44:38 +02:00
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.
2026-05-19 16:44:38 +02:00
*/
class Brand extends Model
{
use HasActivityLog;
use HasCache;
2026-05-19 16:44:38 +02:00
/** @use \Illuminate\Database\Eloquent\Factories\HasFactory<\Database\Factories\BrandFactory> */
use HasFactory;
2026-05-19 16:44:38 +02:00
use HasModelNaming;
use HasTranslations;
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
2026-05-26 13:12:45 +02:00
#[\Override]
2026-05-19 16:44:38 +02:00
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.
/**
2026-05-26 13:12:45 +02:00
* Get relations to include in the cache.
*
2026-05-26 13:12:45 +02:00
* @return list<string>
*/
2026-05-26 13:12:45 +02:00
public static function getCacheRelations(): array
{
return ['sites'];
}
/**
* Manage model fields.
*/
private static function manageFields(self $model): void
{
TranslationHelper::assertTranslationFieldIsUnique($model, $model->name, 'name');
}
2026-05-19 16:44:38 +02:00
// * RELATIONS.
2026-05-19 16:44:38 +02:00
/**
* Get sites associated to the brand.
*
2026-05-26 13:12:45 +02:00
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany<\App\Models\Site,$this,\Illuminate\Database\Eloquent\Relations\MorphPivot,'pivot'>
2026-05-19 16:44:38 +02:00
*/
public function sites(): MorphToMany
2026-05-19 16:44:38 +02:00
{
return $this->morphToMany(
Site::class,
'sitable',
'sitables',
'sitable_id',
'site_id',
)->withPivot('created_at');
}
}