我正在尝试根据表中的其他列计算Laravel中的列,但我不能对它们执行WHERE,因为MySQL不允许这样做。
我想我需要一个子查询。我可以在MySQL中这样做:
attendance
但我不知道如何在Laravel中做同样的事情。我在网上有一些参考资料,但对我来说根本没用。如何将其翻译为Eloquent代码?
答案 0 :(得分:0)
使用DB :: raw作为自定义子查询。
$datas = DB::table( DB::raw('SELECT
@inactive_percentage:=((UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(APP_UPDATE_DATE))) / (UNIX_TIMESTAMP(DEL_TASK_DUE_DATE) - UNIX_TIMESTAMP(APP_UPDATE_DATE)) * 100 AS inactive_percentage,
IF(@inactive_percentage <= 33, "low", IF(@inactive_percentage >= 33 && @inactive_percentage <= 66, "normal", "high")) AS inactive
FROM APP_CACHE_VIEW as foo') )->where('active','low')->get();
then
foreach($datas as $data){
//Your data processing / formatting
}