paginate()在使用hading()时抛出错误,而get()显示结果

时间:2018-07-20 10:55:30

标签: php laravel-5 laravel-5.3 laravel-query-builder laravel-pagination

代码:

if ($request->going){
    if($request->lattitude && $request->longitude){
        $CurrentIplat = $request->lattitude;
        $CurrentIplon = $request->longitude;
        $ads1 = DB::table('ads')
        ->leftJoin('categories', 'ads.category_id', '=', 'categories.id')
        ->select(
            'ads.*','categories.category_name',
            DB::raw("( 6371 * acos( cos( radians('{$CurrentIplat}') ) * cos( radians( ads.Latitude ) ) * cos( radians( ads.Longtitute ) - radians({$CurrentIplon}) ) + sin( radians({$CurrentIplat}) ) * sin( radians( ads.Latitude ) ) ) ) AS distance")
                )
        ->where('ads.domain',$this->domain)
        ->groupBy('ads.id')
        ->orderBy('distance') 
        ->having('distance','<','500')
        ->get();
            }
        }

提供适当的输出。

使用paginate()

if ($request->going){
    if($request->lattitude && $request->longitude){
        $CurrentIplat = $request->lattitude;
        $CurrentIplon = $request->longitude;
        $ads1 = DB::table('ads')
        ->leftJoin('categories', 'ads.category_id', '=', 'categories.id')
        ->select(
            'ads.*','categories.category_name',
            DB::raw("( 6371 * acos( cos( radians('{$CurrentIplat}') ) * cos( radians( ads.Latitude ) ) * cos( radians( ads.Longtitute ) - radians({$CurrentIplon}) ) + sin( radians({$CurrentIplat}) ) * sin( radians( ads.Latitude ) ) ) ) AS distance")
                )
        ->where('ads.domain',$this->domain)
        ->groupBy('ads.id')
        ->orderBy('distance') 
        ->having('distance','<','500')
        ->paginate(10);
            }
        }

它抛出

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'distance' in 'having clause' (SQL: select count(*) as aggregate from `ads` left join `categories` on `ads`.`category_id` = `categories`.`id` where `ads`.`domain` = nz group by `ads`.`id` having `distance` < 5000)

2 个答案:

答案 0 :(得分:1)

Laravel中的

paginate()函数,执行两个查询:

  1. 首先,要获取具有给定的分页限制(例如10)的记录 案件。
  2. 第二,根据给定的位置获取记录总数 条件。

第二个查询出现问题,其中count(*) as aggregate是唯一选择的字段,没有像distance这样的字段可以执行操作。

get()函数起作用,因为它仅执行一个查询以获取具有给定where条件的所有记录。

解决方案:在where中使用距离条件,而不是having,因为它更有效。但是在这里,由于条件不适用于别名,因此需要重新编写相同的距离计算。代码应为:

if ($request->lattitude && $request->longitude) {
    $CurrentIplat = $request->lattitude;
    $CurrentIplon = $request->longitude;
    $queryDistance = " ( 6371 * acos( cos( radians('{$CurrentIplat}') ) * cos( radians( ads.Latitude ) ) * cos( radians( ads.Longtitute ) - radians({$CurrentIplon}) ) + sin( radians({$CurrentIplat}) ) * sin( radians( ads.Latitude ) ) ) ) ";
    $ads1 = DB::table('ads')
            ->leftJoin('categories', 'ads.category_id', '=', 'categories.id')
            ->select('ads.*', 'categories.category_name', DB::raw("$queryDistance AS distance"))
            ->whereRaw('ads.domain = ' . $this->domain . " AND $queryDistance < 500")
            ->groupBy('ads.id')
            ->orderBy('distance')
            ->paginate(10);
}

答案 1 :(得分:0)

使用Epic2RunnerTest.java Laravel首次启动时

paginate

查询以获取记录数,因此,由于在此查询中您还拥有SELECT count(*) ,因此SQL显然不知道此查询中的having('distance', '<', '500')是什么。解决方案是使用手动分页(更复杂的一种)或重复两次距离定义:

distance

因此,如您在上方看到的,$distance = "( 6371 * acos( cos( radians('{$CurrentIplat}') ) * cos( radians( ads.Latitude ) ) * cos( radians( ads.Longtitute ) - radians({$CurrentIplon}) ) + sin( radians({$CurrentIplat}) ) * sin( radians( ads.Latitude ) ) ) )"; if ($request->going){ if($request->lattitude && $request->longitude){ $CurrentIplat = $request->lattitude; $CurrentIplon = $request->longitude; $ads1 = DB::table('ads') ->leftJoin('categories', 'ads.category_id', '=', 'categories.id') ->select( 'ads.*','categories.category_name', DB::raw($distance.' AS distance')) ->where('ads.domain',$this->domain) ->groupBy('ads.id') ->orderBy('distance') ->having(DB::raw($distance),'<','500') ->paginate(10); } } 列被提取到distance变量中,然后使用了两次-$distance中第一次,select中第二次