mysql - Laravel How Make DB raw can inside function
问题描述
我在 laravel 上有问题。我想用 eloquent 显示百分比,但返回空值
这是我的控制器
$group_categories = Proyek::with(['modul' => function($query){
$query->select(DB::raw('((select count(*) from modul where status = 0 and deleted_at IS NULL) /
(select count(*) from modul where deleted_at IS NULL)) *
100 as count' ));
}])->get();
这是 Json 返回
{
id: "10",
proyek: "JuZka",
created_at: "2018-08-12 01:54:04",
updated_at: "2018-09-23 05:49:13",
modul: [ ]
},
解决方案
你可以使用这样的代码:
$all_models = (new Proyek)->count();
$models = (new Proyek)->where('status', 0)->count();
$percentage = $models / $all_models * 100;
$group_categories = Proyek::wheere('modul', $percentage);
推荐阅读
- html - 有没有办法将 onclick 事件应用于 xslt 中的多个值?
- java - SpaceVim jdt-语言-服务器-最新
- angular - SnackBar 在效果规范文件中
- r - Linear regression on split data in R
- python - 合并两个具有重叠范围的数据框并计算按类别分组的重叠
- python - 在购物 django rest 中按类别过滤品牌
- c# - C# WinForm:单击显示帮助工具提示和 Shift+单击显示管理面板登录?
- c# - 为什么这个异步函数需要一个返回值
- c# - C# 按列表项与组排序
- python - getmtime() 与 datetime.now():