2015-10-05 4 views
0

Я пытаюсь с двух часов преобразовать sql-запрос (который извлекает все события вместе с предприятиями, размещенными в радиусе 10 километров от предоставленных координат) в laravel eloquent запросНевозможно преобразовать SQL-запрос в laravel eloquent

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 

то, что я сделал до сих пор является

$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',('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(); 

но выше запрос бросает ошибку, столбец 6371 не найден

+0

честный вопрос, какую выгоду вы пытаетесь получить, преобразовывая запрос в формат красноречия? – castis

+0

Я также хочу, чтобы все участники посетили каждое событие, полученное по вышеуказанному запросу, делая это довольно удобно с красноречивым –

ответ

2

Это не красноречивый запрос, вы просто используете класс laravel DB.

Вы не можете выполнять вычисления/вызовы функций внутри «select» без 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, так что вы просто должны были бы вызвать функцию с latitude и longitude