我是laravel的新手,我正在開發一個應用程序,使用谷歌地圖在用戶搜索位置,然後返回特定半徑的地方。但我陷入瞭如何在Eloquent中寫下代碼。我的查詢是:Laravel Complex Eloquest Query with paginate
$lat = $request->input('lat');
$lng = $request->input('lng');
$dist = $request->input('dist');
$query = "SELECT *, (6371 * acos(cos(radians('$lat')) * cos(radians(lat)) * cos(radians(lng) - radians('$lng')) + sin(radians('$lat')) * sin(radians(lat)))) AS distance FROM locations WHERE lat != '$lat' AND lng != '$lng' HAVING distance <= $dist ORDER BY id DESC";
我已經試過:
$locations = \DB::table('locations')->select(['*', "(6371 * acos(cos(radians('$lat')) * cos(radians(lat)) * cos(radians(lng) - radians('$lng')) + sin(radians('$lat')) * sin(radians(lat)))) as distance"])->paginate(5);
但現在看來,這是行不通的。相反,它顯示此錯誤:
SQLSTATE[42000]: 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 '* from `locations` limit 5 offset 0' at line 1 (SQL: select *, `(` as * from `locations` limit 5 offset 0)
請幫我找出問題。提前致謝。
那麼我該如何在這個方法中應用paginate()?這是拋出錯誤:調用成員函數paginate()在數組 – Pranab
我使用以下工作: \ DB :: table('locations') - > select(\ DB :: raw(「*」 ,(6371 * acos(弧度('$ lat'))* cos(弧度(lat))* cos(弧度(lng) - 弧度('$ lng'))+ sin(弧度('$ lat')) )* sin(radians(lat))))AS distance「)) - > paginate('2'); – Pranab
對於遲到的回覆感到抱歉,所以它現在正在工作?對不起,我只是沒有測試你的情況。 –