【问题标题】:Laravel backpack edit form displaying checklist items with selected values from databaseLaravel 背包编辑表单显示带有从数据库中选择的值的清单项目
【发布时间】:2020-11-06 03:27:19
【问题描述】:

在我的 crud 控制器中,有一个名为“角色(多个清单)”的字段,而在保存角色之前,我正在使用 implode 将数组转换为 1,2,3 之类的字符串。 例如:CrudController setUp()方法

$options = [
                'name' => 'roles',
                'label' => 'Roles',
                'type' => 'checklist',
                'entity' => 'roles',
                'attribute' => 'name',
                'model' => "Backpack\PermissionManager\app\Models\Role",
            
            ];
        
        $this->crud->addField($options);

在 Store 方法中,

public function store(StoreRequest $request)
    {
        $sel_roles = $request->input("roles");

        $roles = !empty($sel_roles) ? implode(",",$sel_roles) : "";

        $request->request->set("roles",$roles);       

        //dd($request);
        return parent::storeCrud($request);
    }

编辑方法是这样的,

public function edit($id) {

        $this->crud->hasAccessOrFail('update');
        // get the info for that entry
         $this->data['entry']= $this->crud->getEntry($id);
         $options = [
            'name' => 'roles',
            'label' => 'Roles',
            'type' => 'checklist',
            'entity' => 'roles',
            'attribute' => 'name',
            'model' => "Backpack\PermissionManager\app\Models\Role",
        
        ];
    
        $this->crud->addField($options);

         $this->data['crud'] = $this->crud;
         $this->data['fields'] = $this->crud->getUpdateFields($id);

         $this->data['id'] = $id;

         return view('crud::edit', $this->data);

    }

如果我试图访问,编辑页面,我得到以下错误,

第 15 行中的错误异常: 在字符串上调用成员函数 pluck()(查看:/var/www/html/app/vendor/backpack/crud/src/resources/views/fields/checklist.blade.php)

checklist.blade.php 页面如下所示

<div @include('crud::inc.field_wrapper_attributes') >
    <label>{!! $field['label'] !!}</label>
    <?php $entity_model = $crud->getModel(); ?>

    <div class="row">
        @foreach ($field['model']::all() as $connected_entity_entry)
            <div class="col-sm-4">
                <div class="checkbox">
                  <label>
                    <input type="checkbox"
                      name="{{ $field['name'] }}[]"
                      value="{{ $connected_entity_entry->id }}"

                      @if( ( old( $field["name"] ) && in_array($connected_entity_entry->id, old( $field["name"])) ) || (isset($field['value']) && in_array($connected_entity_entry->id, $field['value']->pluck('id', 'id')->toArray())))
                             checked = "checked"
                      @endif > {!! $connected_entity_entry->{$field['attribute']} !!}
                  </label>
                </div>
            </div>
        @endforeach
    </div>

    {{-- HINT --}}
    @if (isset($field['hint']))
        <p class="help-block">{!! $field['hint'] !!}</p>
    @endif
</div>

如何在编辑页面中显示具有选定值的角色。

谢谢

【问题讨论】:

  • 错误在checklist.blade.php 内,您没有在问题中显示它
  • checklist.blade.php 供应商背包文件夹中的文件,它需要某种格式。但我无法像在添加页面中那样发送预期的格式。仍然需要该页面,将再次更新checklist页面
  • 期待一个数组...
  • @Raffobaffo,更新清单页面。

标签: php laravel laravel-backpack


【解决方案1】:

经过数小时的调试和验证,

以集合格式发送编辑值,

 $options = [
            'name' => 'role_id',
            'label' => 'Roles',
            'type' => 'checklist',
            'entity' => 'roles',
            'attribute' => 'name',
            'model' => "Backpack\PermissionManager\app\Models\Role",
            "value" => collect([$edit_value_array])
        
        ];

【讨论】:

    猜你喜欢
    • 2019-05-07
    • 1970-01-01
    • 2016-03-27
    • 2014-07-08
    • 1970-01-01
    • 2018-12-01
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多