Laravel:如何从一组集合中创建一个集合


Laravel: How to make one collection out of array of collections?

我使用此查询将id获取到变量中:

$ids = DB::table('note_user')->select('note_id')->where('user_id', $user_id)->get('note_id');

由于我有两张属于一个用户的钞票,我得到了

[{"note_id":1},{"note_id":2}]

这似乎是一个集合数组,我尝试应用的每个Laravel集合方法都会返回一个错误。例如,

$flattened = $ids->flatMap(function ($values) {
    return $values;
});

返回

致命错误:调用数组上的成员函数flatMap()

有什么方法可以打开吗

[{"note_id":1},{"note_id":2}]

进入

[1, 2]

最简单的解决方案是使用pluck

$ids = DB::table('note_user')->select('note_id')->where('user_id', $user_id)->pluck('note_id');

https://laravel.com/api/5.1/Illuminate/Database/Query/Builder.html#method_pluck

这只返回一个像您想要的整数数组。