【问题标题】:How to change Laravel-Nova action name?如何更改 Laravel-Nova 动作名称?
【发布时间】:2019-11-08 10:56:16
【问题描述】:

实际上,我需要更改 laravel nova 动作名称,例如翻译成不同的语言。

class PrintWithDetail extends Action
{
    use InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Perform the action on the given models.
     *
     * @param  \Laravel\Nova\Fields\ActionFields  $fields
     * @param  \Illuminate\Support\Collection  $models
     * @return mixed
     */
    public function handle(ActionFields $fields, Collection $models)
    {
        $id = '';
        //
        foreach ($models as $model) {
            $id = $model->id;
        }
        return Action::openInNewTab(route("print.work.order", [$id, 'type' => 'detail']));
    }

}

上面的动作显示 PrintWithDetail 作为名称。

但是,我需要将 PrintWithDetail 更改为荷兰语。

【问题讨论】:

    标签: laravel laravel-nova


    【解决方案1】:

    覆盖公共属性$name

    /**
     * The displayable name of the action.
     *
     * @var string
     */
    public $name;
    

    所以在你的例子中

    class PrintWithDetail extends Action
    {
        use InteractsWithQueue, Queueable, SerializesModels;
    
        public $name = "Print met detail";
    
        /**
         * Perform the action on the given models.
         *
         * @param  \Laravel\Nova\Fields\ActionFields  $fields
         * @param  \Illuminate\Support\Collection  $models
         * @return mixed
         */
        public function handle(ActionFields $fields, Collection $models)
        {
            $id = '';
            //
            foreach ($models as $model) {
                $id = $model->id;
            }
            return Action::openInNewTab(route("print.work.order", [$id, 'type' => 'detail']));
        }
    }
    

    您可以在nova/src/Actions/Action.php 中的操作扩展类中查看您可以更改的所有内容

    更新

    如果您需要将名称本地化为其他语言,您可以覆盖方法 name 从 Laravel __() 辅助方法返回一个字符串

    /**
     * Get the displayable name of the action.
     *
     * @return string
     */
    public function name()
    {
        return __('Print with details');
    }
    

    【讨论】:

    • 感谢您的时间和精力。顺便说一句,我也像你上面提到的那样做了。
    • @Saly 你能告诉我,如何更改 CRUD 附带的默认前缀“创建/更新”?
    • 我们如何获取名称的关系?就像它是一个数据透视表......
    猜你喜欢
    • 2022-01-09
    • 2019-03-07
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 2019-06-02
    • 2022-01-14
    相关资源
    最近更新 更多