将对象数组的集合转换为数组集合

时间:2018-03-29 10:00:21

标签: laravel collections

我尝试根据foreach中的请求操作集合。 作为一个输出我收到一个带有一系列对象的集合......

我需要的是一个包含我的属性的数组的集合...... 这就是我用dd($ users):

Collection {#551 ▼
  #items: array:16 [▼
    0 => User {#610 ▼
      #table: "users"
      #fillable: array:13 [▶]
      #guarded: array:1 [▶]
      #hidden: array:2 [▶]
      #timestamp: true
      #softDelete: true
      #attributes: array:15 [▼
        "id" => 1
        "ua_id" => 351
        "grade_id" => 584
        "metier_id" => 18
        "nom" => "xxxx"
        "prenom" => "xxxx"
        "matricule" => "xxxx"
        "email" => null
        "datenaissance" => "xxxx"
        "password" => "xxxxx"
         ...

这就是我想要的:

Collection {#2114 ▼
  #items: array:16 [▼
    1 => array:15 [▼
        "id" => 1
        "ua_id" => 351
        "grade_id" => 584
        "metier_id" => 18
        "nom" => "xxxx"
        "prenom" => "xxxx"
        "matricule" => "xxxx"
        "email" => null
        "datenaissance" => "xxxx"
        "password" => "xxxxx"
         ...

1 个答案:

答案 0 :(得分:2)

尝试使用

$ newCollection = collect($ oldCollection-> toArray());

这会将您的旧集合转换为数组,并将此数组转换为集合。