How to make sure that the total input does not exceed a certain number?

Is there a query that retrieves the next-to-user auctions and museums
$query = Location::select(DB::raw("location.id location_label as location, latitude, longitude,
 (6371 * acos( cos( radians($lat) )
 * cos( radians( latitude ) )
 * cos( radians( longitude ) - radians($lng) ) + sin( radians($lat) )
 * sin( radians( latitude ) ) ) )
 AS distance"));

$query->orderBy('distance');

$query = $query->with('auctions:id, location_id')->with('museums:id, location_id');

return $query->get();


In the query result obtained
[
{
 "id": 2,
 "location": "Milan",
 "latitude": "51.5073509",
 "longitude": "-0.1277583",
 "distance": 0,
 "auctions": [
{
 "id": 2,
 "title": "Title",
 "location_id": 2
}
],
 "museums": [
{
 "id": 2,
 "name": "Museum"
 "location_id": "2"
}
]
},
{
 "id": 3,
 "location": "London",
 "latitude": "51.5073509",
 "longitude": "-0.1277503",
 "distance": 0.0005453617665334611,
 "auctions": [],
 "museums": [
{
 "id": 1,
 "name": "Museum"
 "location_id": "3"
}
]
}
]


Need to get the total number of auctions and museums does not exceed 4.

Ie at the front, it will be displayed in the list:
- Аукцион1. Distance 1km.
- Музей1. Distance 1km.
- Музей2. A distance of 1.5 km.
- Аукцион2. A distance of 1.5 km.

And we need to organize a pagination 4 pieces per page.

Is there a way to rewrite the query more optimally under the given task?
Foreach'eat or walk and count the total number of auctions and museums? And if the offset is 10000, it turns out first you need to go over those 10000 auctions/mousam varichem to determine the following 4 entries from this decision, in my opinion.
April 3rd 20 at 18:41
1 answer
April 3rd 20 at 18:43
I can not understand what is the meaning of Location...
Why museums and auctions tied to the city? Ie if I am in London, all the London museums from me at a distance of 1.5 km what? If you need the distance to the user and store the coordinates every Museum and auction. Auctions themselves and the museums are in one table (name, type, coordinates). Distinguish by type. If they have very different fields, it is possible to store them in different tables like this: https://laravel.com/docs/6.x/eloquent-relationship...
In this case, a simple pagination print at least 4, at least 10.

Find more questions by tags Laravel