gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:17:40 25 4
gpt4 key购买 nike

我使用此查询将 id 放入变量中:

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

因为我有两个属于一个用户的笔记,所以我得到了

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

这似乎是一个集合数组,每个 Laravel collection method我尝试应用它返回错误。例如,

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

返回

Fatal error: Call to a member function flatMap() on array

有什么办法可以转

[{"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

这只会返回您想要的整数数组。

关于php - Laravel:如何从一组集合中创建一个集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37084219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com