【发布时间】: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