Skip to content

Support for Laravel strict mode by default defining $localeKey in translation models #453

@nekooee

Description

@nekooee

Describe the bug
When using Laravel's Model::shouldBeStrict() feature, an exception is thrown for any model using the laravel-translatable package if the $localeKey property is not manually defined in the translation model.

To Reproduce

  1. Install astrotomic/laravel-translatable and configure it:
// config/translatable.php
'locale_key' => 'locale',
  1. Enable Laravel strict mode in AppServiceProvider or elsewhere:
use Illuminate\Database\Eloquent\Model;

Model::shouldBeStrict(! app()->isProduction());
  1. Use a model with translations (e.g., NavMenu model).
  2. Attempt to access the model in a context that loads translations.

Expected behavior
The model should work normally under Laravel's strict mode without throwing a MissingAttributeException, assuming configuration is set correctly.

Screenshots

Image

Versions (please complete the following information)

  • PHP: 8.2
  • Database: MySQL 8
  • Laravel: 10.x (also tested on 11.x)
  • Package: astrotomic/laravel-translatable v11.15

Additional context
The issue is resolved by manually adding this to each translation model:

protected $localeKey = 'locale';

It would be ideal if the package defined this property internally (e.g., via trait or base translation model), or documented this requirement for strict mode compatibility.

Exception

Illuminate\Database\Eloquent\MissingAttributeException  
The attribute [localeKey] either does not exist or was not retrieved for model [App\Models\NavMenu].

Stack Trace

#1 Illuminate\Database\Eloquent\Model::getAttribute()
... (Laravel internal calls)

Additional issue with strict mode

When enabling Laravel’s strict mode (Model::shouldBeStrict()), I encountered errors about missing attributes like translationModel and translationForeignKey in translation models.

To temporarily fix these, I had to add the following manually to my main model:

protected string $translationModel = \App\Models\CourseTranslation::class;
protected string $translationForeignKey = 'course_id';

However, this should ideally be handled inside the package to avoid manual fixes in every project.

Could you please provide an approximate timeline for when these strict mode compatibility issues will be officially resolved? It would help us plan and confidently use strict mode with laravel-translatable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions