Laravel - 将数组推送到集合
Laravel - push array to collection
我有这段代码,我试图在其中获取所有授权用户类别:
$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 plugin
您可以像数组一样使用集合:
$cats[5] = 'BMW';
我有这段代码,我试图在其中获取所有授权用户类别:
$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 plugin
您可以像数组一样使用集合:
$cats[5] = 'BMW';