【问题标题】:How to convert Collection of array of arrays to Eloquent style Collection of Objects of Objects in Laravel如何在 Laravel 中将数组集合转换为雄辩风格的对象集合
【发布时间】:2021-09-07 08:28:50
【问题描述】:

我从 Airtable 中提取数据,返回如下:

Illuminate\Support\Collection {#496 ▼
  #items: array:11 [▼
    0 => array:3 [▼
      "id" => "rec4CaAx5EwGGi0I3"
      "fields" => array:8 [▼
        "description" => "Soft covers for switches"
        "price" => 7
        "tags" => array:2 [▼
          0 => "recvPyxXM6GdIjgEr"
          1 => "recvskfZPFMRPXzEb"
        ]

我可以找到如何转换为:

数组对象的集合,甚至

Collection of Collections 使用递归方法,

但不是

Objects of Objects的集合

所以我可以像使用普通 Eloquent 获取的结果一样访问 $data->fields->name 这样的数据

【问题讨论】:

  • 如果对您点击here有帮助,请检查一下

标签: php arrays laravel object collections


【解决方案1】:

原来我可以使用json编码/解码将它全部转换为对象然后收集它@

    $things = Airtable::table('things')->all();

    $things = json_decode(json_encode($things),FALSE);
    
    $things = collect($things);

【讨论】:

    猜你喜欢
    • 2019-05-31
    • 2017-10-24
    • 1970-01-01
    • 2015-03-04
    • 2017-06-30
    • 2015-08-15
    • 1970-01-01
    相关资源
    最近更新 更多