title: How to show stores with nearest long/lat values using search function - Laravel 5 eloquent
body: <p>Ideally I am looking for an Laravel 5 relevant answer. I am trying to make a store locator app. I am up to the point where I'm trying to match one pair of lat/long coordinates (calculated from an address that a user enters into search box) with the nearest (within 100km radius) coordinates/existing stores in my database.</p>
  
  <p>The user enters an address which is converted (using geocoding) into lat and lng coordinates. These are sent to my 'articles' page which has a list of stores &amp; a google map. I used a simple tutorial about scope search to show my articles/stores based on their text 'address'. But this obviously doesn't work for two coordinates. I have a table called Articles which has: id, address, lat, lng, website, title etc..</p>
  
  <p>I need something like <a href="https://stackoverflow.com/questions/17551156/get-the-nearest-longitude-and-latitude-from-mssql-database-table">this</a> or <a href="http://publicvoidlife.blogspot.co.nz/2011/02/shortest-distance-between-two-points-is.html" rel="noreferrer">this</a> but using eloquent. </p>
  
  <p>Current Article Model:</p>
  
  <pre><code>    public function scopeSearch($query, $search){
  
      return $query-&gt;where('address', 'LIKE', "%$search%" );
     }
  </code></pre>
  
  <p>Articles Controller</p>
  
  <pre><code> public function index(Request $request)
  {
       $query = $request-&gt;get('q');
       $articles = $query
      ? Article::search($query)-&gt;get()
      : Article::all();
      return view('articles.index', compact('categories'))-&gt;withArticles($articles);
  }
  </code></pre>
  
  <p>Current form/ geocode:</p>
  
  <pre><code>    {!! Form::open(['method' =&gt; 'GET', 'id' =&gt; 'searchForm', 'name' =&gt; 'searchForm', 'route' =&gt; 'articles_path']) !!}
        {!! Form::input('search', 'q', null, ['placeholder' =&gt; 'LOCATION', 'class' =&gt; 'locationSearch', 'id' =&gt; 'searchInput'])!!}
        {!! Form::hidden('lat', null, ['id' =&gt; 'lat'])!!}
        {!! Form::hidden('lng', null, ['id' =&gt; 'lng'])!!}
        {!! Form::submit('MAKE ME HAPPY', array('id' =&gt; 'submitButton')) !!}
      {!! Form::close() !!}
  
   &lt;script&gt;
  $('#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");
          }
      });
  });
  &lt;/script&gt;  
  </code></pre>