Laravel查询生成器 - 在自定义列上排序

时间:2015-11-05 08:34:33

标签: php mysql laravel laravel-5 query-builder

我想在自定义列(first_name和last_name的concat)上对Laravel查询构建器结果进行排序。

我所做的是 -

    $summary = DB::table('service_rating')
                ->join('partners', 'partners.id',   '=', 'service_rating.partner_id')
                ->join('users',     'users.id',     '=', 'partners.user_id')
                ->select(
                            DB::raw("CONCAT( users.first_name,' ', users.last_name) as lawn_pro"),
                            DB::raw ('AVG(service_rating.rating)                    as rating'),
                            DB::raw ('COUNT(service_rating.rating)                  as jobs'),
                            DB::raw ('SUM(service_rating.rating)                    as payout')
                        )
                ->where('customer_id', '=', Auth::user()->id)
                ->whereRaw('service_rating.created_at >= DATE(NOW()) - INTERVAL '.$no_of_day_to_show.' DAY')
                ->groupBy('service_rating.partner_id')
                ->orderBy('lawn_pro', 'asc');

所以,我收到这条线的错误 -

                ->orderBy('lawn_pro', 'asc');

错误是这样的 -

enter image description here

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

显然您在查询中使用count()函数,这会忽略select属性,因为我们只想知道行的计数。因此,lawn_pro在查询中不可用。

我建议执行查询,然后计算可用的行。

$rows = $summary->get();
$count = count($rows);