Я использую Laravel 5 и имею 2 модели location и chauffeur с отношениями
public function chauffeurs() { return $this->hasMany('App\Chauffeur'); }
а также
public function location() { return $this->hasOne('App\Location', 'id', 'location_id'); }
Это запрос, который у меня есть, который работает
"SELECT *,
(6371 * acos(cos(radians(".$latitude.")) * cos(radians(latitude)) * cos(radians(longitude) - radians(".$longitude.")) + sin(radians(" . $latitude.")) * sin(radians(latitude)))) AS distance
FROM `locations` AS l
JOIN `chauffeurs` AS c
ON (l.id = c.location_id)
HAVING distance < c.radius
ORDER BY distance ";
Это то, что у меня есть до сих пор
Chauffeur::select('*, (6371 * acos(cos(radians(1.492659)) * cos(radians(latitude)) * cos(radians(longitude) - radians(103.7413591)) + sin(radians(1.492659)) * sin(radians(latitude)))) AS distance ')
->join('Location', '`location`.`id`', '=', '`chauffeur`.`location_id`')
->havingRaw('distance < `chauffeur`.`radius`')
->get();
и я получаю эту ошибку
`Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.`492659)) * sin(radians(latitude))))` as `distance ` from `locations` inner joi'`
Как бы я использовал модели laravel для выполнения того же запроса? Любая идея, что я делаю неправильно или есть ли лучший способ? Спасибо






использовать DB::raw(); с запросом выбора
Не используйте эту обратную кавычку в имени таблицы и именах столбцов.