如何使用搜索功能显示具有最近长/纬度值的商店 - Laravel 5 eloquent

tho*_*ism 11 php google-maps laravel-5.1

理想情况下,我正在寻找一个Laravel 5相关答案.我正在尝试制作商店定位器应用程序.我正在尝试将一对纬度/经度坐标(根据用户进入搜索框的地址计算)与我的数据库中最近的(在半径100km范围内)坐标/现有商店相匹配.

用户输入一个地址(使用地理编码)转换为lat和lng坐标.这些内容会发送到我的"文章"页面,其中包含商店列表和谷歌地图.我使用了一个关于范围搜索的简单教程,根据文本"地址"显示我的文章/商店.但这显然不适用于两个坐标.我有一个名为文章的表,其中包括:id,地址,lat,lng,网站,标题等.

我需要像这样或者这样的东西,但是使用雄辩.

当前文章模型:

    public function scopeSearch($query, $search){

    return $query->where('address', 'LIKE', "%$search%" );
   }
Run Code Online (Sandbox Code Playgroud)

文章控制器

 public function index(Request $request)
{
     $query = $request->get('q');
     $articles = $query
    ? Article::search($query)->get()
    : Article::all();
    return view('articles.index', compact('categories'))->withArticles($articles);
}
Run Code Online (Sandbox Code Playgroud)

当前表单/地理编码:

    {!! Form::open(['method' => 'GET', 'id' => 'searchForm', 'name' => 'searchForm', 'route' => 'articles_path']) !!}
      {!! Form::input('search', 'q', null, ['placeholder' => 'LOCATION', 'class' => 'locationSearch', 'id' => 'searchInput'])!!}
      {!! Form::hidden('lat', null, ['id' => 'lat'])!!}
      {!! Form::hidden('lng', null, ['id' => 'lng'])!!}
      {!! Form::submit('MAKE ME HAPPY', array('id' => 'submitButton')) !!}
    {!! Form::close() !!}

 <script>
$('#submitButton').on('click', function(event){
    event.preventDefault();
    var address = $('#searchInput').val();
    geocoder = new google.maps.Geocoder();
    geocoder.geocode({
        'address': address
    }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
             var lat = results[0].geometry.location.lat();
             var lng = results[0].geometry.location.lng();
             $('#lat').val(lat);
             $('#lng').val(lng);
          $('#searchForm').submit();

        } else {
            alert("Geocode was not successful");
        }
    });
});
</script>  
Run Code Online (Sandbox Code Playgroud)

tho*_*ism 11

我最后听了这个建议.我不知道这是否是"雄辩"的方式,但它确实有效.我只是不确定"距离"以及在那里使用什么.

文章模型:

public static function getByDistance($lat, $lng, $distance)
{
  $results = DB::select(DB::raw('SELECT id, ( 3959 * acos( cos( radians(' . $lat . ') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(' . $lng . ') ) + sin( radians(' . $lat .') ) * sin( radians(lat) ) ) ) AS distance FROM articles HAVING distance < ' . $distance . ' ORDER BY distance') );

  return $results;
}
Run Code Online (Sandbox Code Playgroud)

文章控制器

    public function index(Request $request)
{

$lat = $request->get('lat');
$lng = $request->get('lng');
$distance = 1;

$query = Article::getByDistance($lat, $lng, $distance);

    if(empty($query)) {
      return view('articles.index', compact('categories'));
    }

    $ids = [];

    //Extract the id's
    foreach($query as $q)
    {
      array_push($ids, $q->id);
    }

    // Get the listings that match the returned ids
    $results = DB::table('articles')->whereIn( 'id', $ids)->orderBy('rating', 'DESC')->paginate(3);        

    $articles = $results;

    return view('articles.index', compact('categories'))->withArticles($articles);

}
Run Code Online (Sandbox Code Playgroud)


Den*_*bov 5

更好的解决方案是使用此功能作为范围.像那样:

文章模型:

public static function scopeGetByDistance($query,$lat, $lng, $distance)
{
  $results = DB::select(DB::raw('SELECT id, ( 3959 * acos( cos( radians(' . $lat . ') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(' . $lng . ') ) + sin( radians(' . $lat .') ) * sin( radians(lat) ) ) ) AS distance FROM articles HAVING distance < ' . $distance . ' ORDER BY distance') );

    if(!empty($results)) {

        $ids = [];

        //Extract the id's
        foreach($results as $q) {
           array_push($ids, $q->id);
        }

        return $query->whereIn('id',$ids);

    }

    return $query;

 }
Run Code Online (Sandbox Code Playgroud)

文章控制器:

public function index(Request $request) {

    $lat = $request->get('lat');
    $lng = $request->get('lng');
    $distance = 1;

    $articles= Article::scope1()->scope2()->scope3()->GetByDistance($lat, $lng, $distance);

   return $articles; 

}
Run Code Online (Sandbox Code Playgroud)