【问题标题】:Determine selected options based on many-to-many relationship根据多对多关系确定选择的选项
【发布时间】:2021-08-13 14:02:04
【问题描述】:

我有三个表:products、categories 和 category_product。

在我的编辑表单页面中,我在选择框中显示类别和子类别值。 选择的类别选项保存在数据透视表 category_product 中。这个表有product_id和category_id。

这是我的代码。

产品型号

public function category() {
  return $this->belongsToMany('App\Models\Category');
}

类别型号:

public function products()
{
    return $this->belongsToMany(Product::class);
}

编辑产品页面视图:

<select name="category_id[]" class="form-control" multiple size = 10>
   @foreach ($parentCategories as $parentCategory)
       <option value="{{ $parentCategory->id }}">{{ $parentCategory->name }}</option>
       @if(count($parentCategory->subcategory))
         @include('includes.subcats-select',['subcategories' => $parentCategory->subcategory])
       @endif
   @endforeach
</select>

subcats-选择视图

@foreach($subcategories as $subcategory)
  <option value="{{ $subcategory->id }}">---{{ $subcategory->name }}</option>
    @if(count($subcategory->subcategory))
      @include('includes.subcats-select',['subcategories' => $subcategory->subcategory])
    @endif
  </div>
@endforeach

如何将“selected”属性添加到所选类别选项中,以便在编辑页面时正确应用更改?

【问题讨论】:

    标签: php laravel eloquent laravel-8 eloquent-relationship


    【解决方案1】:

    您可以通过以下方式获取pivot table columns

    产品型号

    public function category() {
      return $this->belongsToMany('App\Models\Category')->withPivot('selected');
    }
    

    编辑产品页面视图:

    <select name="category_id[]" class="form-control" multiple size = 10>
       @foreach ($parentCategories as $parentCategory)
           <option value="{{ $parentCategory->id }}" selected="{{ $parentCategory->pivot->selected }}">
               {{ $parentCategory->name }}
           </option>
           @if(count($parentCategory->subcategory))
             @include('includes.subcats-select',['subcategories' => $parentCategory->subcategory])
           @endif
       @endforeach
    </select>
    

    编辑:虽然上述答案可能对其他人有所帮助,但它并不能回答问题。所以这是我在聊天中澄清后的第二次尝试。

    <select name="category_id[]" class="form-control" multiple size = 10>
       @foreach ($parentCategories as $parentCategory)
           <option value="{{ $parentCategory->id }}"{{ $product->category->contains($parentCategory->id) ? 'selected' : '' }}>
               {{ $parentCategory->name }}
           </option>
           @if(count($parentCategory->subcategory))
             @include('includes.subcats-select',['subcategories' => $parentCategory->subcategory])
           @endif
       @endforeach
    </select>
    

    【讨论】:

    • 我已更新产品模型并尝试使用您的代码,但出现此错误:尝试获取非对象的“选定”属性。
    • 所以$parentCategory$product-&gt;category
    • with dd($parentCategories) 我得到了结果,但是有了 dd($product-category) 我得到了 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'category_product.selected' in '字段列表'
    • 如果我从模型中删除 ->withPivot('selected'),dd($product->category) 工作正常
    • selected="{{ $parentCategory->pivot->selected }}" 给出错误屏幕尝试获取非对象的“选定”属性
    猜你喜欢
    • 2012-11-17
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多