Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

abkrim's avatar
Level 13

Translation of BelongsTo in Nova

I use for translations fields in Nova

Text::make(__('Name User'), 'name')

But I don't understand the manual to make the title of the BelongsTo fields translatable

Title Attributes When a BelongsTo field is shown on a resource creation / update screen, a drop-down selection menu or search menu will display the "title" of the resource. For example, a User resource may use the name attribute as its title. Then, when the resource is shown in a BelongsTo selection menu, that attribute will be displayed:

Well... On my code

BelongsTo::make('User')->withMeta([
  'belongsToId' =>  $this->user_id ?? auth()->user()->id
])->hideFromDetail()

Code on Nova\User

public static $model = 'App\User';

public static $title = 'email';

public static $search = ['id', 'name', 'email'];

public static function availableForNavigation(Request $request)
{
    return $request->user()->isAdmin();
}

public static function label()
{
    return __('Users');
}

public static function singularLabel()
{
    return __('User');
}

When show User resource not problem. Labels translations are done.

But if go to resource Mailbox, field BelongsTo not show translations

0 likes
3 replies
abkrim's avatar
Level 13

I forget put this solution on my post.

This solution not work because Nova try to with with Model translation... App\Nova\Usuario instead correct class App\Nova\User

[2020-02-10 19:56:35] local.ERROR: Class 'App\Nova\Usuario' not found {"userId":1,"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Class 'App\Nova\Usuario' not found at /home/abkrim/Sites/albaridnova/vendor/laravel/nova/src/Fields/BelongsTo.php:118)

abkrim's avatar
Level 13

Solution is in doc. It needs 3 parameters, label, table and model.

HasMany::make(__('Mailboxes'), 'mailboxes', Mailbox::class)

Please or to participate in this conversation.