It is quite difficult for me. I have a route that is a search that works quite well, except that I cannot search by zip code to find the closest to lat and longest given zip code. That is, I can work with lat and long, but I'm not sure how to integrate it into my existing request. This query is a search query without postal codes:
$query = DB::table('dogs');
$query->leftJoin('dog_addresses','dogs.id','=','dog_addresses.dog_id');
$query->leftJoin('dog_videos','dogs.id','=','dogs_videos.dog_id');
$query->leftJoin('dogs_breeds','dogs.breed_id','=','dogs_breeds.id');
if($request->input("breed") && $request->input("breed") != "" && $request->input("breed") != "any")
{
$breed = Dog_Breed::where("breed_name", $request->input("breed"))->first();
$query->where('dogs.breed_id', $breed->id);
}
$results = $query->get();
I have something to add to the request to get the latitude and longitude of the zip code:
if($request->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->input('postcode'));
$result = json_decode(curl_exec($curl));
curl_close($curl);
$postcode_lat = $result->result->latitude;
$postcode_long = $result->result->longitude;
}
This allows me to get latent zip code and longitude. But I donβt know how to get dogs by location based on the lat and long columns present in the dog_addresses table, which is connected to the dog table. How to do it?
, dog_addresses Lat Long.
, :
id | user_id | dog_name | age
dog_addresses:
id | dog_id | address_line_1 | town | postcode | lat | long
, , bred ID - 1, , , , , , lat long.
. :
( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance
, .
,