Laravel-将查询结果显示为单行

时间:2019-05-23 10:39:07

标签: laravel

我希望查询结果显示为单数

我已经创建了Controller和视图。

控制器

$actives = DB::table('users')
                         ->selectRaw(' 
                                IF((plan = 1), COUNT(*), "0") as daily, 
                                IF((plan = 2), COUNT(*), "0") as weekly,
                                IF((plan = 3), COUNT(*), "0") as monthly
                         ')
                         ->groupBy('plan')  
                          ->get();

在视图表上,结果显示为六行,并将0添加到其他行。它应该是单行三列。

每日|每周每月一次 5 | 10 | 2

1 个答案:

答案 0 :(得分:0)

尝试使用pluck

$actives = DB::table('users')
     ->selectRaw(' 
            IF((plan = 1), COUNT(*), "0") as daily, 
            IF((plan = 2), COUNT(*), "0") as weekly,
            IF((plan = 3), COUNT(*), "0") as monthly
     ')
     ->groupBy('plan')  
     ->get()
     ->pluck('daily', 'weekly', 'monthly');