Laravel Nova 中 BelongsTo 的翻译

Translation of BelongsTo in Laravel Nova

我在 Nova 中用于翻译字段

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

但我不明白使 BelongsTo 字段的标题可翻译的手册

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:

好吧...在我的代码中,这失败了,因为翻译是在模型名称中生成的,而不是在关系 Ship 的标签中生成的。

BelongsTo::make(__('User'), 'users')->withMeta([
  'belongsToId' =>  $this->user_id ?? auth()->user()->id
])->hideFromDetail()
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)

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');
}

当显示用户资源没有问题。标签翻译完成。

但是如果转到资源邮箱,字段 BelongsTo 不显示翻译

发生这种情况是因为如果你没有在make方法上指定第三个参数(资源),Nova会认为你的资源被调用的方式与标签相同(例如:标签:User 然后 resourceName: User) 并尝试搜索该资源 class.

显然,您可以通过将资源 class 作为第三个参数传递来覆盖它:

// Add the import of your resource if its class
// is not in the same directory as this file
BelongsTo::make(__('User'), 'users', User::class)->withMeta([
  'belongsToId' =>  $this->user_id ?? auth()->user()->id
])->hideFromDetail(),