Laravel - 在运行我的半身像查询后无法链接我的查询

时间:2016-02-18 09:45:00

标签: php mysql laravel query-builder

我根据用户输入过滤API结果,例如:

用户可以说Show me offer的价格在10到50之间,当我发布参数时,我有一个查询来检查用户lat / long和商品表中lat / long之间的距离这一切都很好地运作。

但是,如果我发布lat long参数并传递另一组参数,则查询会中断。例如:

//Working response
{
"offers": [
{
  "id": 1,
  "offer_name": "nah",
  "offer_description": "nah",
  "offer_price": 100,
  "offer_repeat": null,
  "offer_photo": null,
  "offer_lat": "52.754646",
  "offer_long": "-1.251366",
  "offer_date_posted": null,
  "offer_start_date": null,
  "offer_end_date": null,
  "offer_date_repeat": null,
  "student_only": null,
  "offer_amount": "1",
  "updated_at": "-0001-11-30 00:00:00",
  "created_at": null,
  "offer_cat_id": 1,
  "offer_price_type": null,
  "offer_venue_id": 1,
  "offer_location_id": 1,
  "distance": 104.01333767313
},
{
  "id": 2,
  "offer_name": "nope",
  "offer_description": "nope",
  "offer_price": 50,
  "offer_repeat": null,
  "offer_photo": null,
  "offer_lat": "51.754646",
  "offer_long": "-1.251366",
  "offer_date_posted": null,
  "offer_start_date": null,
  "offer_end_date": null,
  "offer_date_repeat": null,
  "student_only": null,
  "offer_amount": "1",
  "updated_at": null,
  "created_at": null,
  "offer_cat_id": 2,
  "offer_price_type": null,
  "offer_venue_id": 1,
  "offer_location_id": 1,
  "distance": 61.474296680605
},
{
  "id": 3,
  "offer_name": "nahn ha",
  "offer_description": "nah",
  "offer_price": 25,
  "offer_repeat": null,
  "offer_photo": null,
  "offer_lat": "51.754646",
  "offer_long": "-1.251366",
  "offer_date_posted": null,
  "offer_start_date": null,
  "offer_end_date": null,
  "offer_date_repeat": null,
  "student_only": null,
  "offer_amount": "1",
  "updated_at": null,
  "created_at": null,
  "offer_cat_id": 3,
  "offer_price_type": null,
  "offer_venue_id": 2,
  "offer_location_id": 2,
  "distance": 61.474296680605
}
]
}

//使用ID<查询5并具有与上述相同的距离参数

{
   "error": {
   "message": "SQLSTATE[HY093]: Invalid parameter number (SQL: select * from                (select *, (69.000000 * DEGREES(ACOS(COS(RADIANS(51.507200)) *       COS(RADIANS(offer_lat)) * COS(RADIANS(0.127500 - offer_long)) +  SIN(RADIANS(51.507200)) * SIN(RADIANS(offer_lat))))) AS distance from `offers`  where `id` < 5 and offer_lat BETWEEN 47.159374 AND 55.855026 and offer_long  BETWEEN -6.857899 AND 7.112899) as d where `id` < 10 and `distance` between 300  and ?)",
    "code": "HY093",
    "status_code": 500
  }
  }

我认为这与我在模型中覆盖$ query变量并仍然返回它的事实有关?如果需要,我可以显示我的模型文件的示例,感谢您的帮助。

如果用户设置了以下参数,则运行查询:(如果您尝试使用此参数设置另一个过滤器,则会中断)

        if (isset($params['lat']) && trim($params['lat']) !== '' && isset($params['long']) && trim($params['long']) !=='' && isset($params['maxdist']) && trim($params['maxdist']) !=='' && isset($params['mindist']) && trim($params['mindist']) !=='')
    {
        $lat = $params['lat'];
        $lng = $params['long'];
        $radius = $params['maxdist'];
        $minRaidus = $params['mindist'];
        $lat = $lat + 0;
        $lng = $lng + 0;

        $units = 'M';

        $distanceUnit = $this->distanceUnit($units);

        if (!(is_numeric($lat) && $lat >= -90 && $lat <= 90)) {
            echo 'fix2';
            var_dump($lat);
        }

        if (!(is_numeric($lng) && $lng >= -180 && $lng <= 180)) {
            echo 'fix1';
        }

        $haversine = sprintf('*, (%f * DEGREES(ACOS(COS(RADIANS(%f)) * COS(RADIANS(offer_lat)) * COS(RADIANS(%f - offer_long)) + SIN(RADIANS(%f)) * SIN(RADIANS(offer_lat))))) AS distance',
            $distanceUnit,
            $lat,
            $lng,
            $lat
        );

        $subselect = clone $query;
        $subselect
            ->selectRaw(DB::raw($haversine));


        $latDistance      = $radius / $distanceUnit;
        $latNorthBoundary = $lat - $latDistance;
        $latSouthBoundary = $lat + $latDistance;
        $subselect->whereRaw(sprintf("offer_lat BETWEEN %f AND %f", $latNorthBoundary, $latSouthBoundary));

        $lngDistance     = $radius / ($distanceUnit * cos(deg2rad($lat)));
        $lngEastBoundary = $lng - $lngDistance;
        $lngWestBoundary = $lng + $lngDistance;
        $subselect->whereRaw(sprintf("offer_long BETWEEN %f AND %f", $lngEastBoundary, $lngWestBoundary));

     $query
            ->from(DB::raw('(' . $subselect->toSql() . ') as d'))
            ->whereBetween('distance', [$minRaidus, $radius]);
    }

为了测试,我只是说ID小于users id参数,这样我可以修复链接时查询中断的距离

    if (isset($params['id']) && trim($params['id']) !== '' )
    {
        $query->where('id', '<', trim($params['id']));
    }

我打电话给我的queryscope:

    if ($request){
        $params = $request->all();
        $offers = Offer::filter($params)->SortByFilter($params)->get();
        return $offers;
    }

0 个答案:

没有答案