使用laravel按经纬度搜索

时间:2018-08-16 05:21:43

标签: php mysql laravel search

我知道这个问题已经被提出了,但就我而言,我遇到了一个令人沮丧的问题,我很乐意找到答案。本质上,当我运行脚本时,遇到一个非数字值,因为我无法在方程式中使用db表中的行值。请看下面:

$auctions = Auction::join('users','auctions.user_id','users.id')
                ->join('categories','auctions.category_id','categories.id')
                ->join('sub_catogories','auctions.sub_category_id','sub_catogories.id')
                ->select(['auctions.id','auctions.title','auctions.slug',
                          'auctions.description','auctions.image',
                          'auctions.reserve_price','auctions.auction_status',
                          'auctions.start_date','auctions.end_date'], 
                          ((acos(sin($orig_lat * pi() / 180) * sin(-37.8136 * pi() / 180) + cos($orig_lat * pi() / 180) * cos(-37.8136 * pi() / 180) * cos(($orig_long - '**HOW DO I INSERT users.lat HERE????**') * pi() / 180)) * 180 / pi()) * 60 * 1.1515), 'AS', 'distance')

                ->where ( 'lat', 'BETWEEN', ($orig_lat - $bounding_distance), 'AND', ($orig_lat +         $bounding_distance), 'AND', 'lng', 'BETWEEN', ($orig_long - $bounding_distance), 'AND', ($orig_long + $bounding_distance))
                ->orderBy('auctions.id','desc')->paginate(PAGINATE_RECORDS);

1 个答案:

答案 0 :(得分:0)

此错误将消失,而不是使用select而不是selectRaw

Auction::join('users','auctions.user_id','users.id')
                ->join('categories','auctions.category_id','categories.id')
                -
>join('sub_catogories','auctions.sub_category_id','sub_catogories.id')
                ->selectRaw('auctions.id','auctions.title','auctions.slug',
                          'auctions.description','auctions.image',
                          'auctions.reserve_price','auctions.auction_status',
                          'auctions.start_date','auctions.end_date', 
                          ((acos(sin($orig_lat * pi() / 180) * sin(-37.8136 * pi() / 180) + cos($orig_lat * pi() / 180) * cos(-37.8136 * pi() / 180) * cos(($orig_long - '**HOW DO I INSERT users.lat HERE????**') * pi() / 180)) * 180 / pi()) * 60 * 1.1515), 'AS', 'distance')

                ->where ( 'lat', 'BETWEEN', ($orig_lat - $bounding_distance), 'AND', ($orig_lat +         $bounding_distance), 'AND', 'lng', 'BETWEEN', ($orig_long - $bounding_distance), 'AND', ($orig_long + $bounding_distance))
                ->orderBy('auctions.id','desc')->paginate(PAGINATE_RECORDS);