如何从Laravel 5.5中的原始表达式查询生成器解决结果 [英] how to solve result from raw expression query builder in laravel 5.5
问题描述
我怎样才能得到结果
100.0%
100.0%
在Laravel blade.php中?当我在下面的laravel 5.5中使用代码原始表达式时:
in Laravel blade.php? When I use code raw expression in laravel 5.5 below:
$jml_status_ap_all_pr =DB::table('aps')->select(DB::raw('ROUND((COUNT(STATUS)/(SELECT
COUNT(*) FROM aps))*100,1) as calc'))->get();
我得到这个结果:
[{"calc":"100.0"}]%
[{"calc":"100.0"}] %
在blade.php中
in blade.php
推荐答案
\Illuminate\Database\Query\Builder
的->get()
方法返回\Illuminate\Support\Collection
的实例,在您的情况下,只需获取一个结果.为此,请使用->first() method instead of ->get()
. ->first()
方法的stdClass
返回实例,您可以直接获取所需的属性
The ->get()
method of \Illuminate\Database\Query\Builder
return instance of \Illuminate\Support\Collection
, In your case need get only one result. For this use ->first() method instead of ->get()
. ->first()
method return instance of stdClass
and you can directly get your needed attribute
对于您的情况,更正的代码是
For your case corrected code is.
$jml_status_ap_all_pr = DB::table('aps')
->select(DB::raw('ROUND((COUNT(STATUS)/(SELECT COUNT(*) FROM aps))*100,1) as calc'))
->first()
->calc;
这篇关于如何从Laravel 5.5中的原始表达式查询生成器解决结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!