我写了这个:
$properties = DB::table('properties')
->join('addresses', 'properties.id_address_fk', '=', 'addresses.id')
->select('properties.id', 'title', 'city', 'price', 'postedat',
( '3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians (-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) as distance') );
但是在阅读错误通知时,当我写“作为距离”时,我并没有正确地接受Haversie查询。也就是说,我需要该别名以便能够在之后进行过滤:这是错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '3959' in 'field list' (SQL: select `properties`.`id`, `title`, `city`, `price`, `postedat`, `3959` as `acos(` from `properties` inner join `addresses` on `properties`.`id_address_fk` = `addresses`.`id` where `propertytype` = house having `distance` < 40 order by `distance` desc limit 5 offset 10)
这就是我需要别名的原因:
$properties->having('distance', '<', $radius)
->orderBy('distance', 'desc')
->skip(10)
->take(5)
->get();
答案 0 :(得分:0)
尝试使用DB::raw()
使Laravel将字符串直接插入查询而不转义。
$properties = DB::table('properties')
->join('addresses', 'properties.id_address_fk', '=', 'addresses.id')
->select('properties.id', 'title', 'city', 'price', 'postedat',
DB::raw('3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians (-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) as distance') );