Laravel. How to combine the query into the model?

Have a request model:
$lat = (float) 55;
$lng = (float) 37;
$city = $lat.','$lng
$locations = Locality::where('point', $city)
->with('rating_results')
 ->firstOrFail();

And in this model it is necessary to make another request:
$nearest = \DB::table('some')->select(\DB::raw("*,
 (6371 * ACOS(COS(RADIANS($lat))
 * COS(RADIANS(latitude))
 * COS(RADIANS($lng) - RADIANS(longitude))
 + SIN(RADIANS($lat))
 * SIN(RADIANS(latitude)))) AS distance")
 )->orderBy('distance', 'asc')->take(20)->get();


How to do this in one query?
June 26th 19 at 13:59
1 answer
June 26th 19 at 14:01
The question is asked incorrectly. You want to combine the first request with a second? If so, your first query returns only one column.

Approximately
$lat = (float) 55;
$lng = (float) 37;
$city = $lat.','$lng;

$nearest = \DB::table('some')
 ->where('point', $city)
 // join under the correct table structure.
->join('rating_results','some.id','rating_results.local_id')
->select(\DB::raw("*,
 (6371 * ACOS(COS(RADIANS($lat))
 * COS(RADIANS(latitude))
 * COS(RADIANS($lng) - RADIANS(longitude))
 + SIN(RADIANS($lat))
 * SIN(RADIANS(latitude)))) AS distance")
)
 ->orderBy('distance', 'asc')->take(20)->get();

Find more questions by tags Laravel