【问题标题】:Laravel - push array to collectionLaravel - 将数组推送到集合
【发布时间】:2018-09-23 03:53:39
【问题描述】:

我有这段代码,我尝试在其中获取所有身份验证用户类别:

$cats = Auth::user()->cats()->lists('title','id');

我想向 $cats 添加新数据,所以我写:

 $cats->push(['5','BMW']);

但我得到了:

    Collection {#459 ▼
  #items: array:2 [▼
    9 => "asd"
    10 => array:2 [▼
      0 => "5"
      1 => "BMW"
    ]
  ]
}

我如何更改我的代码以获得此结果:

Collection {#459 ▼
  #items: array:2 [▼
    9 => "asd"
    5 => "BMW"
  ]
}

那么我怎样才能将数组添加到这个集合中呢? p.s.我需要这种格式,因为我使用 select2 jquery 插件

【问题讨论】:

  • 新数据真的是['5','BMW']吗?还是['5' => 'BMW']
  • ['5' => '宝马']...
  • 使用->put(5, ‘BMW’)

标签: php arrays laravel collections push


【解决方案1】:

你可以像数组一样使用集合:

$cats[5] = 'BMW';

【讨论】:

    猜你喜欢
    • 2018-01-16
    • 2016-04-09
    • 2016-03-23
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多