首页 > 解决方案 > Laravel - 将查询结果显示为单行

问题描述

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

我已经创建了控制器和视图。

控制器

$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

标签: laravel

解决方案


试试这个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');

推荐阅读