簡體   English   中英

Laravel-如何使用雄辯的ORM一次在4個表中選擇數據?

[英]Laravel - How can select data in 4 tables at a time using Eloquent ORM?

我有4個表: productsattributesattribute_groupsattribute_product

產品-屬性:多對多。

屬性-屬性組:一對多。

使用雄辯的ORM,我如何獲取嵌套對象以顯示類似的數據:

@foreach($product->attributeGroups as $attributeGroup)

    <div class="form-group">
        <label>{{ $attributeGroup->name }}:</label>
        {{ Form::select('attributes[$attributeGroup->id][]', $attributeGroup->attributes, null, ['class' => 'form-control input-sm']) }}
    </div>

@endforeach

在您的產品模型中,只需添加以下內容

class Product extends Eloquent {
     protected $with = ['attributeGroups.attributes'];

     public function attributeGroups() {
          return $this->belongsToMany('AttributeGroup');
     }

}

這將自動/渴望通過產品資源模型的每個請求加載您的關系。

這是AttributeGroup雄辯模型的外觀示例。

class AttributeGroup extends Eloquent {
      protected $table = 'attribute_groups';

      public function attributes() {
           return $this->belongsToMany('Attribute');
      }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM