laravel - 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
解决方案
试试这个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');
推荐阅读
- ubuntu - 从端点写入或读取超时,以及通信格式问题(wireshark + pyusb)
- python - 通过组合框更改选项卡,反之亦然
- javascript - 如何在 Gridsome 页面/模板中嵌入 Wistia 频道视频库
- react-native - 如何从类组件调用堆栈导航器?
- sql-server - 能否使用 System Center Data Protection Manager 备份 Azure DevOps Server SQL Server 数据库?
- sql - ORACLE SQL中获取BLOB的图像格式
- java - 四舍五入与所有预期结果不匹配
- python - 列表理解用字典中的键字符串替换字符串中的值
- mysql - SELECT 语句中的 MYSQL 和 TOP 用法
- ios - 在 Info.plist 中配置场景时不使用 AppDelegate