【问题标题】:Translation of BelongsTo in Laravel NovaBelongsTo 在 Laravel Nova 中的翻译
【发布时间】:2020-05-27 21:28:35
【问题描述】:

我在 Nova 中用于翻译字段

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

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

标题属性 当 BelongsTo 字段显示在资源创建/更新屏幕上时,下拉选择菜单或搜索菜单将显示资源的“标题”。例如,用户资源可以使用名称属性作为其标题。然后,当资源显示在 BelongsTo 选择菜单中时,将显示该属性:

嗯...在我的代码中,这失败了,因为翻译是在模型的名称中产生的,而不是在关系 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 不显示翻译

【问题讨论】:

    标签: laravel laravel-nova


    【解决方案1】:

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

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

    // 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(),
    

    【讨论】:

    • 非常感谢。在查看文档后,我已经纠正了我的问题。我看到需要传递三个参数,Label、table和model。谢谢@mdexp
    • 没问题,将来您还可以检查源代码,这些源代码确实有据可查且“雄辩”。有时查看源代码比查看文档更快。无论如何,如果你觉得我的答案是正确的,或者自己发布一个标记为正确的,因为这可能会帮助其他人在未来遇到同样的问题
    猜你喜欢
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 2022-08-05
    • 2021-03-19
    • 1970-01-01
    • 2019-12-08
    • 2023-04-04
    • 2019-04-28
    相关资源
    最近更新 更多