【问题标题】:Laravel - Filtering and Grouping Collections with different TypesLaravel - 过滤和分组不同类型的集合
【发布时间】:2018-03-02 16:49:53
【问题描述】:

我有两种不同类型的集合。

Collection {#277 ▼
  #items: array:2 [▼
    "" => Collection {#271 ▼
      #items: array:2 [▼
        0 => {#265 ▼
          +"id": 3
          +"name": "Product 3"
          +"cover_image": ""
        }
        1 => {#273 ▼
          +"id": 9
          +"name": "Product 9"
          +"cover_image": ""
        }
      ]
    }
    1 => Collection {#272 ▼
      #items: array:1 [▼
        0 => SuperProducts {#282 ▼
          +timestamps: true
          #table: "super_products"
          #fillable: array:6 [▶]
          #connection: "mysql"
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #attributes: array:9 [▶]
          #original: array:9 [▶]
          #casts: []
          #dates: []
          #dateFormat: null
          #appends: []
          #events: []
          #observables: []
          #relations: []
          #touches: []
          #hidden: []
          #visible: []
          #guarded: array:1 [▶]
        }
      ]
    }
  ]
}

第一个集合是一个数组转换为集合,没有使用 Eloquent。其次是超级产品系列。两个集合使用合并组合在一起。有没有办法可以按类型对这两个不同的集合进行分组?或者也许有更好的解决方案?

【问题讨论】:

    标签: laravel laravel-5 collections eloquent


    【解决方案1】:

    您是否尝试从您的 SuperProducts 加载子项目? 如果是这种情况,您应该使用这样的雄辩关系加载您的 SuperProducts 的主要集合和您的子项目:

    App\SuperProducts::with('name_of_your_relation') [..]
    

    如果不是这种情况,您应该像这样将结果附加到您的集合中:https://laravel.com/docs/5.5/eloquent-serialization#appending-values-to-json

    希望对你有帮助

    【讨论】:

    • 基本上它有两个不同的集合,一个从数组转换而来,另一个从模型转换而来。所以我想序列化它们,但不确定如何
    • 第一个集合,数组,是动态生成的还是静态的?
    猜你喜欢
    • 2016-05-09
    • 1970-01-01
    • 2018-05-07
    • 2013-11-04
    • 2011-01-14
    • 1970-01-01
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多