C
C
chelkaz2017-05-11 22:17:28
Laravel
chelkaz, 2017-05-11 22:17:28

Laravel. How to combine a query into a model?

There is a model request:

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

And in this model, you need to immediately make another request:
$nearest = \DB::table('localities')->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 it in one request?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question