如何在数据透视表中使用额外列

时间:2017-02-24 00:09:14

标签: crud custom-field-type backpack-for-laravel

我需要像清单一样制作自定义字段,但需要在表格和第二个字段中输入" order"。
这就是我所拥有的:

  • 模型1:有关系的条件" belongsToMany"
  • 模型2:子服务

这两个模型与表products_service有关系,product_service有两个foreign_keys加上列" order"

我无法理解的是:
如何用背包保存额外的信息。

我知道我可以使用这样的东西:

$user->roles()->updateExistingPivot($roleId, $attributes);

但我应该把它放在哪里?

这是我的代码:

class Condition extends Model
{
  use CrudTrait;
  use Searchable;

  public function relService()
  {
    //dd($this->belongsToMany(SubService::class, 'conditions_service')->withPivot('weight')->withTimestamps());
            return $this->belongsToMany(SubService::class, 'conditions_service')->withPivot('weight')->withTimestamps();
  }
}

class SubService extends Model
{
  use Searchable;
  use CrudTrait;

  public function conditions()
  {
    return $this->belongsToMany(Condition::class, 'conditions_service')->withPivot('weight')->withTimestamps();
  }
}

这是我的自定义字段类型:

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

    <div class="row">
        <div class="col-sm-12">
            <table id="crudTable" class="table table-bordered table-striped display">
                <thead>
                <tr>
                    @if ($crud->details_row)
                        <th></th> <!-- expand/minimize button column -->
                    @endif

                    {{-- Table columns --}}
                    <th>Seleziona</th>
                    <th>Ordine</th>
                    {{--<th>Ordine <i class="fa fa-arrow-up" aria-hidden="true"></i></th>
                    <th>Ordine <i class="fa fa-arrow-down" aria-hidden="true"></i></th>--}}
                    {{--$tst = $connected_entity_entry->relService->whereIn('id', $connected_entity_entry->id)--}}
                </tr>
                </thead>
                <tbody>
                @foreach ($field['model']::all() as $connected_entity_entry) {{--var_dump((empty($connected_entity_entry->conditions->toArray())?"puppa":(empty($connected_entity_entry->conditions->whereIn('id', $connected_entity_entry->id)->toArray())?"puppa uguale":$connected_entity_entry->conditions->whereIn('id', $connected_entity_entry->id)))) --}}
                {{-- dump($connected_entity_entry->getPriority($connected_entity_entry->id)) --}}
                <tr>
                    <th scope="row">
                        <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>
                    </th>
                    <td>{{--@include('crud::fields.number')--}}

                    </td>
                </tr>
                @endforeach
                </tbody>
            </table>
        </div>

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

感谢您的帮助! 戴夫

1 个答案:

答案 0 :(得分:1)

我找到了一个解决方案,感谢密集团队的拉取请求(https://github.com/Laravel-Backpack/CRUD/pull/351

这是我的代码更新:

CrudController:

obj.That

这是我的自定义字段类型:

$this->crud->addField([
        'label'     => 'Servizi legati',
        'type'      => 'checklist_ordered',
        'name'      => 'relService',
        'entity'    => 'relService',
        'attribute' => 'title',
        'model'     => "App\Models\SubService",
        'pivot'     => true,
        'pivotFields' => [
            'weight' => 'Ordinamento',
        ],
    ], 'update/create/both');
相关问题