title: get by latitude and longitude in laravel 5 with other joins
body: <p>This is quite a complicated one for me. I have a route that is a search - which works quite well, except I am unable to search by postcode to find the nearest to the lat and long of a given postcode. That is, I can work out the lat and long, but I am not sure how to integrate it to my existing query. This query is the search query without postcodes:</p>
  
  <pre><code>$query = DB::table('dogs');
  $query-&gt;leftJoin('dog_addresses','dogs.id','=','dog_addresses.dog_id');
  $query-&gt;leftJoin('dog_videos','dogs.id','=','dogs_videos.dog_id');
  $query-&gt;leftJoin('dogs_breeds','dogs.breed_id','=','dogs_breeds.id');
  if($request-&gt;input("breed") &amp;&amp; $request-&gt;input("breed") != "" &amp;&amp; $request-&gt;input("breed") != "any")
      {
          $breed = Dog_Breed::where("breed_name", $request-&gt;input("breed"))-&gt;first();
          $query-&gt;where('dogs.breed_id', $breed-&gt;id);
      }
  $results = $query-&gt;get();
  </code></pre>
  
  <p>I have something to add to the query to get the latitude and longitude of the postcode:</p>
  
  <pre><code>if($request-&gt;input("postcode")) 
  {
      $curl = curl_init();
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl, CURLOPT_URL, "http://api.postcodes.io/postcodes/" . $request-&gt;input('postcode'));
      $result = json_decode(curl_exec($curl));
      curl_close($curl);
      $postcode_lat = $result-&gt;result-&gt;latitude;
      $postcode_long = $result-&gt;result-&gt;longitude;            
  }
  </code></pre>
  
  <p>This lets me get my postcode latitute and longitude.
  But I don't then know how to get the dogs by location based on the lat and long columns present in the dog_addresses table, which is joined to the dogs table. How do I do this?</p>
  
  <p>So if my dog_addresses table has the columns Lat and Long.</p>
  
  <p>So dogs:</p>
  
  <pre><code>id | user_id | dog_name | age
  </code></pre>
  
  <p>dog_addresses:</p>
  
  <pre><code>id | dog_id | address_line_1 | town | postcode | lat | long
  </code></pre>
  
  <p>So for my query I need to get all dogs, where bred ID is 1, but I want to inner join videos so I can get all video information and addresses information, but I also want to sort the order of dogs returned by how close they are to my inputted postcode, based on lat and long.</p>
  
  <p>I'm very confused. I found this:</p>
  
  <pre><code>( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance
  </code></pre>
  
  <p>But I'm still not sure how to integrate it, or what use it is for me.
  Please help</p>