如何在Laravel中通过键表单集合进行访问

时间:2018-08-10 21:56:08

标签: php group-by laravel-5.6 array-map laravel-collection

我在 laravel 5.6 中有一个收集数据。我想在其中检索所有键和值形式的多维数组,并将我的收集结果自定义为关联数组。因此我使用了 collect 方法,但是由于多维数组无法访问。

看看我的收款结果。

[
{
    "id": 1,     
    "follow": 1,
    "subscriber_followed": {
        "id": 1,
        "name": "Sipes LLC",          
        "status": "saved",
        "updated_at": "July 28, 2018",
        "status_update": [
            {
                "id": 133,
                "project_id": 1,
                "status": {
                    "id": 1,
                    "parent_id": null,
                    "name": "Pre-Construction"
                },
                "sub_status": {
                    "id": 11,
                    "parent_id": 1,
                    "name": "Design WIP"
                },
                "date": "August 16, 2018",
                "status_update": "under construction substructure 100% on 1/6/2018"
            },
            {
                "id": 134,
                "project_id": 1,
                "status": {
                    "id": 1,
                    "parent_id": null,
                    "name": "Pre-Construction"
                },
                "sub_status": {
                    "id": 12,
                    "parent_id": 1,
                    "name": "Tendering WIP"
                },
                "date": "August 16, 2018",
                "status_update": "pre construction Design WIP on 3/7/17"
            }
        ]
    }
}

], ....

这是我的收集结果。因此之后,我想将此结果自定义为这种形式。在这种形式下,我想按** date形式status_update **分组。

[
   {
       "name": "Sipes LLC",       
       "status_update": "under construction substructure 100% on 1/6/2018",     
       "date": "August 16, 2018"
   }
 ]

解决方案在我这边,但有一个在下面提到的问题。

我找到了自己的解决方案。这是最简单的方法,但如果有任何改进的话,应该由其他方法进行修改。

$data = collect($project)->map( function($q){
        $data['name'] = $q['subscriber_followed']['name'];
        $data=  collect($q['subscriber_followed']['statusUpdate']) 
           ->map(function($statusUpdate) use($data)
             {
                $data['date'] = $statusUpdate['date'];
                $data['status_update'] =$statusUpdate['status_update'];
                return $data ;

              })->sortByDesc('date');
                return $data;
         })->flatten(1);

所以之后我们将以这种方式获得结果

{
 "data": [
    {
        "name": "LC",
        "date": "August 16, 2018",
        "status_update": "under construction substructure 100% on 1/6/2018"
    },
    {
        "name": "Schinner, Oberbrunner and Turcotte",
        "date": "August 16, 2018",
        "status_update": "pre construction Design WIP on 3/7/17"
    },
    {
        "name": "Sipes LLC",
        "date": "August 16, 2018",
        "status_update": "under construction substructure 100% on 1/6/2018"
    },
    {
        "name": "Schinner, Oberbrunner and Turcotte",
        "date": "August 16, 2018",
        "status_update": "pre construction Design WIP on 3/7/17"
    }
 ]
}

但是在这里,我面临的一个问题是按名称日期进行分组。所以当我分组然后以这种方式获得结果

{
"data": {
    "Sipes LLC": [
        {
            "name": "Sipes LLC",
            "date": "August 16, 2018",
            "status_update": "under construction substructure 100% on 1/6/2018"
        },
        {
            "name": "Sipes LLC",
            "date": "August 16, 2018",
            "status_update": "under construction substructure 100% on 1/6/2018"
        }
    ],
    "Schinner, Oberbrunner and Turcotte": [
        {
            "name": "Schinner, Oberbrunner and Turcotte",
            "date": "August 16, 2018",
            "status_update": "pre construction Design WIP on 3/7/17"
        },
        {
            "name": "Schinner, Oberbrunner and Turcotte",
            "date": "August 16, 2018",
            "status_update": "pre construction Design WIP on 3/7/17"
        }
    ]
 }
}

因此这里存在一个问题。如何访问此响应,因为 Sipes LLC 是项目名称,并且是动态的。因此每次都会有所不同。也可以是数字形式。手段 就像这样。

{
 "data": {
    1: [
      {
        "name": "Sipes LLC",
        "date": "August 16, 2018",
        "status_update": "under construction substructure 100% on 1/6/2018"
      },
      {
        "name": "Sipes LLC",
        "date": "August 16, 2018",
        "status_update": "under construction substructure 100% on 1/6/2018"
      }
  ],
   2: [
      {
        "name": "Schinner, Oberbrunner and Turcotte",
        "date": "August 16, 2018",
        "status_update": "pre construction Design WIP on 3/7/17"
      },
      {
        "name": "Schinner, Oberbrunner and Turcotte",
        "date": "August 16, 2018",
        "status_update": "pre construction Design WIP on 3/7/17"
      }
    ]
  }
}

所以我需要这种方式。我怎样才能做到这一点?

我如何按名称和日期对它们进行分组,并且应采用数字索引形式?

0 个答案:

没有答案