Name aus der locale-gefiltert geladenen Uebersetzung mit #id-Fallback als Model-Methode. Ersetzt die identische Inline-Logik an 4 Stellen (Footer-Codes und Kategorien jeweils create/edit). Test: CategoryLocalizedNameTest. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
68 lines
1.7 KiB
PHP
68 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Portal;
|
|
use Database\Factories\CategoryFactory;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Category extends Model
|
|
{
|
|
/** @use HasFactory<CategoryFactory> */
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'parent_id',
|
|
'portal',
|
|
'is_active',
|
|
'legacy_portal',
|
|
'legacy_id',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'portal' => Portal::class,
|
|
'is_active' => 'boolean',
|
|
];
|
|
}
|
|
|
|
public function parent(): BelongsTo
|
|
{
|
|
return $this->belongsTo(self::class, 'parent_id');
|
|
}
|
|
|
|
public function children(): HasMany
|
|
{
|
|
return $this->hasMany(self::class, 'parent_id');
|
|
}
|
|
|
|
public function translations(): HasMany
|
|
{
|
|
return $this->hasMany(CategoryTranslation::class);
|
|
}
|
|
|
|
/**
|
|
* Name aus der bereits (locale-gefiltert) geladenen Übersetzung, mit
|
|
* `#id`-Fallback, falls keine Übersetzung vorliegt. Erwartet eine
|
|
* eager-geladene `translations`-Relation der gewünschten Sprache.
|
|
*/
|
|
public function localizedName(): string
|
|
{
|
|
return $this->translations->first()?->name ?? '#'.$this->id;
|
|
}
|
|
|
|
public function pressReleases(): HasMany
|
|
{
|
|
return $this->hasMany(PressRelease::class);
|
|
}
|
|
|
|
public function footerCodes(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(FooterCode::class, 'category_footer_code');
|
|
}
|
|
}
|