无法将SQL查询转换为laravel eloquent

时间:2015-10-05 17:32:26

标签: php sql laravel-5 eloquent laravel-5.1

我已经尝试了几个小时来转换一个sql查询(它将所有事件以及在提供的坐标的10公里半径范围内托管的业务)转换为laravel eloquent查询

$stateProvider
.state('oku', {
url: "/oku/:name/:id",
views: {
"viewC": { templateUrl: "oku.html",
            controller: "nbgCtrl",},
},
   resolve: {
     alData : function(NBG, $stateParams, $filter){
        return NBG.adlar.then(function(res){
            return $filter('filter')(res.data, {chapter: $stateParams.id}, true)[0];
        });
     }
   }
 })

.controller('nbgCtrl', function($scope, alData, NBG) {
$scope.images = alData;
NBG.adlar.success(function(verHemen){
   $scope.bilgiler = verHemen;
})
})

到目前为止我所做的是

SELECT businesses.id AS business_id,
       businesses.name AS business_name,
       businesses.area AS business_area,
       businesses.address AS business_address,
       businesses.city AS business_city,
       businesses.country AS business_country,
       businesses.longitude AS business_longitude,
       businesses.latitude AS latitude,
       businesses.postcode AS business_postcode,
       businesses.category AS business_category,
       businesses.phone AS business_phone,
       businesses.timing AS business_timing,
       businesses.owner_id AS business_owner_id,
       businesses.latitude AS business_latitude,
       businesses.longitude AS business_longitude,
       events.id AS event_id,
       events.title AS event_title,
       events.type AS event_type,
       events.event_type AS event_event_type,
       events.time AS event_time,
       events.description AS event_description,
       events.venue AS event_venue,
       events.visibility AS event_visibility,
       events.picture AS event_picture,
       events.max_people AS event_max_people,
       events.admin_id AS event_admin_id,
       6371 * (acos(cos(radians (" . $latitude . ")) * cos(radians(`latitude`)) * cos(radians(`longitude`) - radians(" . $longitude . ")) + sin( radians(" . $latitude . ")) * sin(radians(`latitude`)))) AS distance
FROM businesses
JOIN events ON businesses.id = events.business_id
WHERE events.created_at > '" . $date . "'
  OR events.updated_at > '" . $date . "'
  OR businesses.created_at > '" . $date . "'
  OR businesses.updated_at > '" . $date . "'
HAVING distance <= 10 

但是上面提到的查询抛出了未找到列6371的错误

1 个答案:

答案 0 :(得分:2)

这不是一个雄辩的查询,你只需使用laravel DB类。

您无法在&#39;选择&#39;内进行计算/功能调用。没有DB::raw,因为它会将其视为列名称,这就是您获取6371 not found的原因。

    $locations = DB::table('businesses')
        ->select('id as business_id', 'name as business_name','area as business_area', 'address as business_address',
            'city as business_city', 'country as business_country','longitude as business_longitude',
            'latitude as latitude','postcode as business_postcode', 'category as business_category',
            'phone as business_phone','timing as business_timing', 'owner_id as business_owner_id',
            'latitude as business_latitude', 'longitude as business_longitude',DB::raw('6371 * (acos( cos(radians
                (' . $latitude . ') ) * cos( radians( `latitude` ) ) * cos( radians( `longitude` ) - radians(' . $longitude . ') ) + sin(
                    radians(' . $latitude . ') ) * sin( radians( `latitude` )) ) ) AS distance'))
        ->join('events', 'events.business_id', '=', 'businesses.id')
        ->select('events.id as event_id', 'events.title as event_title', 'type as event_type', 'event_type as event_event_type',
            'time as event_time', 'description as event_description', 'venue as event_venue','visibility as event_visibility',
            'picture as event_picture', 'max_people as event_max_people', 'admin_id as event_admin_id')
        ->get();

BTW而不是这样做 -

6371 * (acos(cos(radians (" . $latitude . ")) * cos(radians(latitude)) * cos(radians(longitude) - radians(" . $longitude . ")) + sin( radians(" . $latitude . ")) * sin(radians(latitude)))) AS distance

每次你想测量距离时你都可以创建一个mysql函数,所以你只需要用latitudelongitude

来调用函数