Lumen的Laravel查询生成器

时间:2016-08-21 07:21:12

标签: laravel lumen

当我使用Laravel的查询构建器将我的Lumen应用程序与MySQL数据库一起使用时,它无法按预期工作。我用过:

$itemid = DB::table('table1')->where('UserID','=',1)->pluck('ID'); 

这只返回一个值。这里有什么错误?

2 个答案:

答案 0 :(得分:3)

尝试

$itemid = DB::table('table1')->where('UserID','=',1)->get()->pluck('ID'); 

在这里,您可以阅读更多有关使用pluck on query时发生这种情况的原因 Pluck together with first using Query builder

更新: 我忘了DB :: table返回数组,所以:

$items = DB::table('table1')->where('UserID','=',1)->get();
$itemsById = array_pluck($items, 'ID');

答案 1 :(得分:1)

使用first而不是get as get return array。

$ itemid = DB :: table('table1') - > where('UserID','=',1) - > first() - > pluck('ID');