如何重新格式化拉拉维尔关系集合



我有以下模型及其关系:

产品 - (名称、价格、数量(

产品属性(数量,价格 - 当有组合时,这里的数量和价格是得到的(

属性 -(大小、颜色(

属性值 -(小、中、大、红色、蓝色、绿色等(

// One to Many Relationship
$product->productAttributes (returns a collection)
// Many to Many Relationship
$productAttribute->attributeValues (returns a collection)
// One to Many Relationship
$attribute->attributeValues

我需要以这样的方式格式化它:

array:2 [
  "color" => array:2 [
    0 => "red"
    1 => "blue"
  ]
  "size" => array:3 [
    0 => "small"
    1 => "medium"
    2 => "large"
  ]
]

这是我在迭代集合时得到的:

    $attributes = $productAttributes->map(function (ProductAttribute $pa){
        return $pa->attributesValues->map(function (AttributeValue $av) {
            return [$av->attribute->name => $av->value];
        });
    })->all();

它显然返回:

array:6 [▼
  0 => Collection {#508 ▼
    #items: array:2 [▼
      0 => array:1 [▼
        "Color" => "red"
      ]
      1 => array:1 [▼
        "Size" => "small"
      ]
    ]
  }
  1 => Collection {#525 ▼
    #items: array:2 [▼
      0 => array:1 [▼
        "Color" => "red"
      ]
      1 => array:1 [▼
        "Size" => "medium"
      ]
    ]
  }
  2 => Collection {#535 ▼
    #items: array:2 [▼
      0 => array:1 [▼
        "Color" => "red"
      ]
      1 => array:1 [▼
        "Size" => "large"
      ]
    ]
  }
  3 => Collection {#545 ▼
    #items: array:2 [▼
      0 => array:1 [▼
        "Color" => "red"
      ]
      1 => array:1 [▼
        "Size" => "small"
      ]
    ]
  }
]

同样,我的目的是将其格式化为:

array:2 [
  "color" => array:2 [
    0 => "red"
    1 => "blue"
  ]
  "size" => array:3 [
    0 => "small"
    1 => "medium"
    2 => "large"
  ]
]

啪!

编辑:

$product->productAttributes
    Collection {#513 ▼
      #items: array:6 [▼
        0 => ProductAttribute {#514 ▼
          #fillable: array:2 [▶]
          #connection: "mysql"
          #table: null
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #attributes: array:6 [▶]
          #original: array:6 [▼
            "id" => 7
            "quantity" => 3
            "price" => "100.00"
            "product_id" => 25
            "created_at" => "2018-03-11 16:56:36"
            "updated_at" => "2018-03-11 16:56:36"
          ]
          #changes: []
          #casts: []
          #dates: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: []
          #touches: []
          +timestamps: true
          #hidden: []
          #visible: []
          #guarded: array:1 [▶]
        }
        1 => ProductAttribute {#515 ▶}
        2 => ProductAttribute {#516 ▶}
        3 => ProductAttribute {#517 ▶}
        4 => ProductAttribute {#518 ▶}
        5 => ProductAttribute {#519 ▶}
      ]
    }
    $productAttribute->attributeValues
array:6 [▼
  0 => Collection {#507 ▼
    #items: array:2 [▼
      0 => AttributeValue {#521 ▼
        #fillable: array:1 [▶]
        #connection: "mysql"
        #table: null
        #primaryKey: "id"
        #keyType: "int"
        +incrementing: true
        #with: []
        #withCount: []
        #perPage: 15
        +exists: true
        +wasRecentlyCreated: false
        #attributes: array:5 [▶]
        #original: array:7 [▼
          "id" => 1
          "value" => "red"
          "attribute_id" => 1
          "created_at" => "2018-03-11 16:49:15"
          "updated_at" => "2018-03-11 16:49:15"
          "pivot_product_attribute_id" => 7
          "pivot_attribute_value_id" => 1
        ]
        #changes: []
        #casts: []
        #dates: []
        #dateFormat: null
        #appends: []
        #dispatchesEvents: []
        #observables: []
        #relations: array:1 [▶]
        #touches: []
        +timestamps: true
        #hidden: []
        #visible: []
        #guarded: array:1 [▶]
      }
      1 => AttributeValue {#506 ▼
        #fillable: array:1 [▶]
        #connection: "mysql"
        #table: null
        #primaryKey: "id"
        #keyType: "int"
        +incrementing: true
        #with: []
        #withCount: []
        #perPage: 15
        +exists: true
        +wasRecentlyCreated: false
        #attributes: array:5 [▶]
        #original: array:7 [▶]
        #changes: []
        #casts: []
        #dates: []
        #dateFormat: null
        #appends: []
        #dispatchesEvents: []
        #observables: []
        #relations: array:1 [▶]
        #touches: []
        +timestamps: true
        #hidden: []
        #visible: []
        #guarded: array:1 [▶]
      }
    ]
  }
  1 => Collection {#523 ▶}
  2 => Collection {#530 ▶}
  3 => Collection {#537 ▶}
  4 => Collection {#544 ▶}
  5 => Collection {#547 ▶}
]

我认为,您正在寻找的内容如下所示;

$attributes = $productAttributes->pluck('attributesValue')
    ->flatten()
    ->unique()
    ->groupBy(function (AttributeValue $av) {
        return $av->attribute->name;
    })
    ->each(function (Collection $avs) {
        $avs->map(function (AttributeValue $av) {
            return $av->value;
        });
    });

这是上面的代码的作用;

  • 拉动关系attributesValue以获得所有结果。
  • 将其展平,以便我们有一个包含所有attributesValue的集合
  • 确保我们没有任何重复项
  • 我们将所有内容分组,以便我们得到类似colour => (collection of colour attributesValue)
  • 现在,我们循环访问多级集合,并映射AttributeValue实例,以便将每个实例替换为其值

我自己没有运行过这段代码,但我相信这会起作用。

最新更新