Answer the question
In order to leave comments, you need to log in
How to make sure that the total number of nested resources does not exceed a certain number?
There is a query that selects auctions and museums closest to the user
$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();
[
{
"id": 2,
"location": "Milan",
"latitude": "51.5073509",
"longitude": "-0.1277583",
"distance": 0,
"auctions": [
{
"id": 2,
"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"
}
]
}
]
Answer the question
In order to leave comments, you need to log in
I can't understand the meaning of Location...
Why are museums and auctions tied to a city? Those. if I am in London, then all London museums are at a distance of 1.5 km from me, or what? If you need the distance to the user, then store your coordinates for each museum and auction. The auctions and museums themselves are in the same table (name, type, coordinates). Distinguish by type. If they have very different fields, then you can store them in different tables like this:
https ://laravel.com/docs/6.x/eloquent-relationship...
ten.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question