Laravel Collection Where 使用注意事项

laravel 的collection功能非常强大并且易用,其中where是最常用的函数之一

先看一下官网的用法:

$collection = collect([
    ['product' => 'Desk', 'price' => 200],
    ['product' => 'Chair', 'price' => 100],
    ['product' => 'Bookcase', 'price' => 150],
    ['product' => 'Door', 'price' => 100],
]);

$filtered = $collection->where('price', 100);

$filtered->all();

/*
[
    ['product' => 'Chair', 'price' => 100],
    ['product' => 'Door', 'price' => 100],
]
*/

注意到,where是会返回原数组元素中的key=>value的

但是对于eloquent查询出来的结果,通常我们只关注其中的value的,这时候的key如果不注意就可能带来一些副作用。

例如,在构建一个json api的时候想要返回类似于下面的数据,就可能产生这样的问题(对于只有一个元素的idle_info会额外保存了key,变成了object, 对于没有或有两个以上元素的则正常显示为array):

{
  "message": "请求成功!",
  "data": [
    {
      "id": 1,
      "code": "officiis",
      "name": "omnis",
      "name_en": "exercitationem",
      "remark": null,
      "idle_info": []
    },
    {
      "id": 2,
      "code": "distinctio",
      "name": "velit",
      "name_en": "placeat",
      "remark": null,
      "idle_info": [  //两个元素正常
        {
          "id": 17,
          "dye_factory_id": 1,
          "dye_factory_name": "maiores",
          "machine_type_id": 2,
          "machine_type_name": "ut",
          "year": 2017,
          "month": 2,
          "idle_capacity": 190,
          "dye_factory": {
            "id": 1,
            "code": "eum",
            "name": "pariatur",
            "name_en": "quo",
            "type_id": 2,
            "province_id": 8,
            "city_id": 8,
            "town_id": 2,
            "product_type_id": 4,
            "contact": "debitis",
            "address": "consequatur",
            "delivery_address": "dolorem",
            "remark": null,
            "is_checked": 0,
            "last_check_date": "1980-11-24 00:00:00"
          }
        },
        {
          "id": 31,
          "dye_factory_id": 1,
          "dye_factory_name": "consequatur",
          "machine_type_id": 2,
          "machine_type_name": "et",
          "year": 2017,
          "month": 2,
          "idle_capacity": 21328.162,
          "dye_factory": {
            "id": 1,
            "code": "eum",
            "name": "pariatur",
            "name_en": "quo",
            "type_id": 2,
            "province_id": 8,
            "city_id": 8,
            "town_id": 2,
            "product_type_id": 4,
            "contact": "debitis",
            "address": "consequatur",
            "delivery_address": "dolorem",
            "remark": null,
            "is_checked": 0,
            "last_check_date": "1980-11-24 00:00:00"
          }
        }
      ]
    },
    {
      "id": 3,
      "code": "voluptatem",
      "name": "iure",
      "name_en": "voluptatem",
      "remark": null,
      "idle_info": []  //没有元素正常
    },
    {
      "id": 10,
      "code": "dolor",
      "name": "quibusdam",
      "name_en": "omnis",
      "remark": null,
      "idle_info": {  //一个元素,保留了key,转换成了object
        "2": {
          "id": 23,
          "dye_factory_id": 1,
          "dye_factory_name": "facilis",
          "machine_type_id": 10,
          "machine_type_name": "non",
          "year": 2017,
          "month": 2,
          "idle_capacity": 3.1,
          "dye_factory": {
            "id": 1,
            "code": "eum",
            "name": "pariatur",
            "name_en": "quo",
            "type_id": 2,
            "province_id": 8,
            "city_id": 8,
            "town_id": 2,
            "product_type_id": 4,
            "contact": "debitis",
            "address": "consequatur",
            "delivery_address": "dolorem",
            "remark": null,
            "is_checked": 0,
            "last_check_date": "1980-11-24 00:00:00"
          }
        }
      }
    }
  ]
}

解决办法:只保留value值即可

//$item['idle_info'] = $capacities->where('machine_type_id', $item->id);

$item['idle_info'] = array_values($capacities->where('machine_type_id', $item->id)->toArray());
            

你可能感兴趣的:(Laravel Collection Where 使用注意事项)