从经纬10km以下的Sql中获取用户

实际上我是 laravel 的新手,我尝试从 10 公里纬度和经度以下的 Sql 获取用户,但它的抛出错误。错误是——


方法 Illuminate\Support\Collection::SQRT 不存在。


这是我的控制器代码


<?php


namespace App\Http\Controllers;


use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

use Illuminate\Foundation\Bus\DispatchesJobs;

use Illuminate\Foundation\Validation\ValidatesRequests;

use Illuminate\Routing\Controller as BaseController;

use DB;


class Controller extends BaseController

{

    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function index()

    {

        $users = DB::table('users')

        ->select('id','name','phone','latitude','longitude')

        ->get()

        ->SQRT("POW(69.1 * (latitude - 24.900110), 2) +

        POW(69.1 * (67.099760 - longitude) * COS(latitude / 57.3), 2)) AS distance")

        ->having("distance < 25")

        ->orderBy("distance");


         return view('userview', compact('users'));


    }

}


三国纷争
浏览 79回答 1
1回答

子衿沉夜

你可以试试这个——$users&nbsp;=&nbsp;DB::table('users') &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;->select(DB::raw('id,name,phone,&nbsp;SQRT(POW(69.1&nbsp;*&nbsp;(latitude&nbsp;-&nbsp;24.900110),&nbsp;2)&nbsp;+&nbsp;POW(69.1&nbsp;*&nbsp;(67.099760&nbsp;-longitude)&nbsp;*&nbsp;COS(latitude&nbsp;/&nbsp;57.3),&nbsp;2))&nbsp;AS&nbsp;distance')) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;->havingRaw('distance&nbsp;<&nbsp;25') &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;->OrderBy('distance') &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;->get();
打开App,查看更多内容
随时随地看视频慕课网APP