Skip to content

HasTranslation ​

Namespace: Unusualify\Modularous\Entities\Traits\HasTranslation

Extends Astrotomic\Translatable\Translatable with Modularous-aware overrides for locale-keyed attribute filling, attribute transformation, and translation class resolution.


Boot Behavior ​

EventAction
savingWhen model is a Pivot, runs handleTranslationAttributes to route locale-keyed arrays correctly
deleting / forceDeletingCalls deleteTranslations() to remove all translation records

Configuration ​

php
// In your model
public $translatedAttributes = ['title', 'body', 'slug'];

// Enable transformed fill (locale-keyed arrays)
protected bool $transformTranslatedAttributes = true;

Methods ​

MethodSignatureDescription
fill(array $attributes): staticOverrides parent fill to route locale-keyed arrays through handleTranslationAttributes
setAttribute(string $key, mixed $value): staticFalls through to Translatable for translated keys; parent for non-translated
translatedAttribute(string $key, ?string $locale = null): mixedReturns the translated value for a key; without locale returns a Collection of all translations
getTranslatedAttribute(string $key, ?string $locale = null): mixedReturns the translated value for the current (or given) locale via translate()
getTranslatedAttributes(): arrayReturns $this->translatedAttributes
getTranslationModelNameDefault(): stringResolves the Translation model class ({Model}Translation) from the module or capsule namespace
getActiveLanguages(): CollectionReturns all configured locales with their published status for this model
hasActiveTranslation(?string $locale = null): boolReturns true if the model has an active translation for the locale
disableTranslationFilling(): voidTemporarily disables locale-keyed fill routing
enableTranslationFilling(): voidRe-enables locale-keyed fill routing

Scopes ​

ScopeDescription
scopeWithActiveTranslations(?string $locale)Eager-loads active translations for the given locale
scopeOrderByTranslation(string $key, string $dir = 'ASC', ?string $locale)Orders by a translated column via JOIN
scopeOrderByRawByTranslation(string $rawOrder, string $groupBy, ?string $locale)Orders by a raw expression on the translations table

Usage ​

php
use Unusualify\Modularous\Entities\Traits\HasTranslation;

class Article extends Model
{
    use HasTranslation;

    public $translatedAttributes = ['title', 'body'];
}

// Filling translations
$article->fill([
    'en' => ['title' => 'Hello', 'body' => 'World'],
    'fr' => ['title' => 'Bonjour', 'body' => 'Monde'],
]);
$article->save();

// Reading
$article->title;                            // current locale
$article->translatedAttribute('title', 'fr'); // 'Bonjour'
$article->getActiveLanguages();
// [['value' => 'en', 'published' => true], ...]

// Checking
$article->hasActiveTranslation('fr');       // true

// Querying
Article::withActiveTranslations()->get();
Article::withActiveTranslations('de')->get();
Article::orderByTranslation('title')->get();