22 lines
455 B
PHP
22 lines
455 B
PHP
<?php
|
|
|
|
namespace App\Models\Traits;
|
|
|
|
trait HasLocalizedName
|
|
{
|
|
//
|
|
public function getNameAttribute()
|
|
{
|
|
$locale = app()->getLocale(); // e.g. 'en' or 'fr'
|
|
// Default fallback language
|
|
$column = 'name_en';
|
|
|
|
if (in_array($locale, ['en', 'kh'])) {
|
|
$column = "name_{$locale}";
|
|
}
|
|
|
|
// Return localized name if exists, else fallback
|
|
return $this->{$column} ?? $this->name_en;
|
|
}
|
|
}
|