You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Laravel Backpack子字段中使用关联可变形多选组件?

在Laravel Backpack子字段中配置关联可变形选择组件

要将关联可变形选择组件应用到Backpack的子字段中,不能直接使用链式调用的addMorphOption方法,需要将所有配置以数组形式嵌套在子字段定义内。以下是具体实现方案:

完整配置代码

假设你的父字段是子字段容器(比如repeatable或table类型),子字段中添加可变形选择组件的代码如下:

CRUD::field('items')
    ->label('子字段容器')
    ->type('repeatable') // 根据实际使用的子字段容器类型调整
    ->fields([
        [
            'name'          => 'colorable',
            'label'         => trans('translate.to'),
            'type'          => 'morph', // 指定字段类型为morph
            'morph_options' => [
                // 配置第一个可变形选项
                'App\Models\PlexiColor' => [
                    'label' => trans("translate.plexi_color"),
                    'field' => [
                        'type'                    => 'select2_from_ajax',
                        'attribute'               => "name",
                        'entity'                  => 'colorable',
                        'model'                   => "App\Models\PlexiColor",
                        'data_source'             => backpack_url('board-color-price/fetch/plexi-color'),
                        'minimum_input_length'    => 0,
                        'method'                  => 'POST',
                        'include_all_form_fields' => true,
                    ]
                ],
                // 第二个可变形选项
                'App\Models\MultiStyleColor' => [
                    'label' => trans("translate.multistyle_colors"),
                    'field' => [
                        'type'                    => 'select2_from_ajax',
                        'attribute'               => "name",
                        'entity'                  => 'colorable',
                        'model'                   => "App\Models\MultiStyleColor",
                        'data_source'             => backpack_url('board-color-price/fetch/multistyle-color'),
                        'minimum_input_length'    => 0,
                        'method'                  => 'POST',
                        'include_all_form_fields' => true,
                    ]
                ],
                // 第三个可变形选项
                'App\Models\StealColor' => [
                    'label' => trans("translate.steal_colors"),
                    'field' => [
                        'type'                    => 'select2_from_ajax',
                        'attribute'               => "name",
                        'entity'                  => 'colorable',
                        'model'                   => "App\Models\StealColor",
                        'data_source'             => backpack_url('board-color-price/fetch/steal-color'),
                        'minimum_input_length'    => 0,
                        'method'                  => 'POST',
                        'include_all_form_fields' => true,
                    ]
                ],
                // 第四个可变形选项
                'App\Models\PleximirrorColor' => [
                    'label' => trans("translate.Pleximirror_colors"),
                    'field' => [
                        'type'                    => 'select2_from_ajax',
                        'attribute'               => "name",
                        'entity'                  => 'colorable',
                        'model'                   => "App\Models\PleximirrorColor",
                        'data_source'             => backpack_url('board-color-price/fetch/pleximirror-colors'),
                        'minimum_input_length'    => 0,
                        'method'                  => 'POST',
                        'include_all_form_fields' => true,
                    ]
                ]
            ],
            // 配置morph类型字段的样式
            'morph_type_field' => [
                'wrapper' => ['class' => 'form-group col-md-6']
            ],
            // 配置morph ID字段的样式和占位符
            'morph_id_field' => [
                'wrapper' => ['class' => 'form-group col-md-6'],
                'placeholder' => trans('translate.choose')
            ]
        ]
    ]);

关键说明

  • 子字段必须指定type为morph,替代原来的链式调用字段定义
  • 所有可变形选项通过morph_options数组配置,键为模型类名,值包含label和对应的field配置(即原addMorphOption的第三个参数)
  • morph_type_field和morph_id_field直接作为子字段数组的键值对配置,不再使用链式调用方法

内容的提问来源于stack exchange,提问作者Mostafa Jamali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 08:47:45