[Collection] 求助教主

教主您好,我在做一个购物车的功能。 通过laravel的关联查询我得到如下数据:

[
    {
        "id": 1,
        "uid": 1,
        "product_id": 10,
        "product_type": 1,
        "buynum": 4,
        "good": {
            "id": 10,
            "good_sn": "TJD-2016-001",
            "good_name": "农家土鸡蛋",
            "shop": {
                "id": 2,
                "user_id": 2,
                "name": "影视度假村欢",
                "company": "影视度假村欢"
            }
        }
    },
    {
        "id": 2,
        "uid": 1,
        "product_id": 11,
        "product_type": 1,
        "buynum": 1,
        "good": {
            "id": 11,
            "good_sn": "TYD-2016-002",
            "good_name": "农家土鸭蛋",
            "shop": {
                "id": 2,
                "user_id": 2,
                "name": "影视度假村欢",
                "company": "影视度假村欢"
            }
        }
    },
    {
        "id": 3,
        "uid": 1,
        "product_id": 12,
        "product_type": 1,
        "buynum": 1,
        "good": {
            "id": 12,
            "good_sn": "sg-2016-002",
            "good_name": "西夏水果王",
            "shop": {
                "id": 1,
                "user_id": 1,
                "name": "鲜果世家休闲农业观光园",
                "company": "鲜果世家休闲农业观光园"
            }
        }
    }
]

但是我需要将数据转化成:

array:2 [
  2 => array:28 [
    "id" => 2
    "user_id" => 2
    "name" => "影视度假村欢"
    "company" => "影视度假村欢" 
    "goods" => array:2 [
      0 => array:7 [
        "id" => 1
        "good_sn" => "TJD-2016-001"
        "good_name" => "农家土鸡蛋" 
        "uid" => 1
        "product_id" => 10
        "product_type" => 1
        "buynum" => 4 
      ]
      1 => array:7 [
        "id" => 2
        "good_sn" => "TYD-2016-002"
        "good_name" => "农家土鸭蛋" 
        "uid" => 1
        "product_id" => 11
        "product_type" => 1
        "buynum" => 1 
      ]
    ]
  ]
  1 => array:28 [
    "id" => 1
    "user_id" => 1
    "name" => "鲜果世家休闲农业观光园"
    "company" => "鲜果世家休闲农业观光园" 
    "goods" => array:1 [
      0 => array:7 [
        "id" => 3
        "good_sn" => "sg-2016-002"
        "good_name" => "西夏水果王" 
        "uid" => 1
        "product_id" => 12
        "product_type" => 1
        "buynum" => 1
      ]
    ]
  ]
]

也就是按照shop分组来取。 当然,通过其他的方式也能实现。但是看到您的视频,想通过collect实现下。最终未能实现。请您给帮帮忙~

另外您的在视频中讲的都是单独的数据。实际和laravel的结合您能不能在出些视频?

JellyBool
修改的评论也不能少于六个字哦!
yurenzhen 回复 JellyBool
修改的评论也不能少于六个字哦!
JellyBool 回复 yurenzhen
修改的评论也不能少于六个字哦!
JellyBool
修改的评论也不能少于六个字哦!
yurenzhen 回复 JellyBool
修改的评论也不能少于六个字哦!
JellyBool 回复 yurenzhen
修改的评论也不能少于六个字哦!
yurenzhen 回复 JellyBool
修改的评论也不能少于六个字哦!
JellyBool 回复 yurenzhen
修改的评论也不能少于六个字哦!
yurenzhen 回复 JellyBool
修改的评论也不能少于六个字哦!
yurenzhen 回复 JellyBool
修改的评论也不能少于六个字哦!
happyqian
修改的评论也不能少于六个字哦!
yurenzhen 回复 happyqian
修改的评论也不能少于六个字哦!
wangsong1234
修改的评论也不能少于六个字哦!