如何在同一个表中使用两个数组的同步?

时间:2017-01-20 08:50:37

标签: laravel laravel-5 laravel-5.2

在视图中,我有一个复选框列表:

 @foreach($features as $key => $feature)
   <div class="col-md-4 col-sm-6 col-xs-12">
     <div class="featuresCheckbox business">
        <input  v-model="property_credentials.features" type="checkbox" name="features[]" value="{{ $feature->id }}" id="test{{ $key }}", {{in_array($feature->id,$property_features)?"checked":""}} />
          <label for="test{{ $key }}" class="checkboxTitle featuresTitle">{{ $feature->name }}</label>
     </div>
</div>
@endforeach

此外,我还输入了一些其他内容:

<div v-for="field in fields" class="additional_fields">
   <label>
     <input name="additional_features[]" type="checkbox" value="@{{ field.value }}" checked>
    @{{ field.value }}
    <button type="button" class="btn btn-xs fa fa-close" @click="deleteCheckbox(field)"></button>
    </label>
</div>

现在我需要的是同步这两个数组,以便我在数据透视表中获取用户从复选框中选择的所有内容以及输入内容。

我试过这样的事情,但当我删除它不起作用时:

  $all_ids= [];
  $all = [];
  if(!empty($existing_additional_features)){
     $all_ids = array_merge($existing_additional_features,$additional_ids);
  }
  else{
    $all_ids = $additional_ids;
  }
  if(!empty($features)){
    $all = array_merge($features,$all_ids );
  }
  else{
    $all = $all_ids;
  }
  if(!empty($all)){

      $property->features()->sync($all);
  }
  else{
    $property->features()->detach();
  }

0 个答案:

没有答案