Serialization
toArray(), toJson(), response()->json($model) and API Resources emit the resolved locale value for each translatable attribute — identical to $model->name — not the raw stored JSON map:
app()->setLocale('en');
$topic->toArray()['name']; // "Investing" (localized, through the fallback chain)
$topic->toJson(); // {"name":"Investing", …}
TopicResource::make($topic); // {"name":"Investing"}
response()->json($topic); // same resolved valuesThe effective FallbackMode is honoured: a model in None mode with only sk set serializes name as null for an en request. Persisted data is untouched — this changes output shape only.
Exposing the full map
When an admin screen needs every locale, call getTranslations() explicitly — for example in a dedicated resource:
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
final class AdminTopicResource extends JsonResource
{
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'name' => $this->getTranslations('name'), // ['en' => '…', 'sk' => '…']
'missing' => $this->missingTranslations(), // field => missing locales
'completeness' => $this->translationCompleteness(), // 0.0–1.0
];
}
}Show your open-source love
This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.
More ways to support, including cryptoBy donating, you agree to our donation terms.
Want this built into your product?
We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.