laravel如何使db raw能在函数内运行

svmlkihl  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(334)

我对拉威尔有意见。我想用雄辩的方式显示百分比,但返回空值
这是我的控制器

$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: [ ]
},
7kjnsjlb

7kjnsjlb1#

您可以使用如下代码:

$all_models = (new Proyek)->count();
$models = (new Proyek)->where('status', 0)->count();
$percentage = $models / $all_models * 100;
$group_categories = Proyek::wheere('modul', $percentage);

相关问题