Laravel 4 Laravel 4基于与其他2个模型的关系查找集合

Laravel 4 Laravel 4基于与其他2个模型的关系查找集合,laravel-4,eloquent,Laravel 4,Eloquent,我有这样的模型: //return $product_type->products; $product_type->load('products')->products; // returns Eloquent Collection of products // or shorter equivalent: $product_type->products; //return $product_type->attribute_types; the same as a

我有这样的模型:

//return $product_type->products;
$product_type->load('products')->products; // returns Eloquent Collection of products
// or shorter equivalent:
$product_type->products;

//return $product_type->attribute_types;
the same as above since the relationships are identical (many-to-many)


// Then to get Attributes:
$product_type->load('products.attributes');
//then you can get all attributes as a single array (every attr as an array not model):
$attributesArray = $product_type->products->fetch('attributes')->collapse();

// or without casting merge all Attributes in a loop:
$attributesCollection = new \Illuminate\Database\Eloquent\Collection;
foreach ($product_type->products as $product)
{
  $attributesCollection = $attributesCollection->merge($product->attributes);
}
  • 有一个产品类型表(产品类型)
  • 有一个与产品类型表具有多对一关系的产品表
  • 有一个属性类型表(属性类型)
  • 在产品类型和属性类型之间有一个(多对多)关系表
  • 有一个包含属性类型、属性值的表(属性)
  • 在产品和属性之间有一个(多对多)关系表
  • 对于这个系列,我需要做一些主要的事情

    $product_type = ProductType::find(1);
    
    查找某个产品类型的所有产品

    return $product_type->products;
    
    查找产品类型的所有属性类型

    return $product_type->attribute_types;
    
    查找属性类型和产品类型的所有属性。我知道这里的问题是我需要属性是产品类型和属性类型的子级


    但是,我不确定这样做的最佳方式。

    设置文档中的关系

    然后,您可以按如下方式获得所需的收藏:

    //return $product_type->products;
    $product_type->load('products')->products; // returns Eloquent Collection of products
    // or shorter equivalent:
    $product_type->products;
    
    //return $product_type->attribute_types;
    the same as above since the relationships are identical (many-to-many)
    
    
    // Then to get Attributes:
    $product_type->load('products.attributes');
    //then you can get all attributes as a single array (every attr as an array not model):
    $attributesArray = $product_type->products->fetch('attributes')->collapse();
    
    // or without casting merge all Attributes in a loop:
    $attributesCollection = new \Illuminate\Database\Eloquent\Collection;
    foreach ($product_type->products as $product)
    {
      $attributesCollection = $attributesCollection->merge($product->attributes);
    }
    

    现在,只需要找出基于属性过滤集合的最佳方法。如果这是可以添加到答案中的内容,我认为它对许多人都会有用。我想过滤查询中的属性会更容易。你需要什么样的过滤器?