Laravel-如何将不同的状态码视为计数 [英] Laravel - How to treat different status code as count
问题描述
我正在尝试将Larave-5.8用于带有状态码status_code的chartjs饼图
I am trying to use Larave-5.8 for chartjs pie chart with a field status_code
$status_record = EmployeeApplication::selectRaw('count(status_code) as count,status_code, if (status_code = 1, "Pending", "Treated") as status')->whereNotIn('employee_code', $publishedgoals)->where('hr_response', 0)->groupBy('status_code')->get();
我编写的代码可用于两个项目
The code I have written on works for two items
status_code是tinyint,可以具有0、1、2、3
status_code is tinyint which can have 0,1,2,3
我希望它添加所有这些
-
0 =草稿
0 = Draft
1 =待处理
2 =已治疗
3 =未经治疗
表中整个字段的数量= TotalSubmitted
count of the whole field in the table = TotalSubmitted
我如何修改
selectRaw('count(status_code)作为count,status_code,如果(status_code = 1,"Pending","Treated")作为status')
selectRaw('count(status_code) as count,status_code, if (status_code = 1, "Pending", "Treated") as status')
在上面的代码中实现了吗?
in the code above to achieve this?
推荐答案
您可以使用
会在指定的条件下返回值,对于您而言(无双关),它会返回状态 status_code
.
which returns a value on a specified condition, In your case (no pun intended) it returns the status of status_code
.
$status_record = EmployeeApplication::selectRaw('count(status_code) as count,CASE
WHEN status_code= 0 THEN \'Draft\'
WHEN status_code= 1 THEN \'Pending\'
WHEN status_code= 2 THEN \'Treated\'
WHEN status_code= 3 THEN \'Untreated\'
ELSE \'TotalSubmitted\'
END as status')
->whereNotIn('employee_code', $publishedgoals)->where('hr_response', 0)
->groupBy('status')->get();
这篇关于Laravel-如何将不同的状态码视为计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!