Reputation: 18869
I'm finally learning the Laravel ORM, and quite frankly, it's making me depressed as I'm working against a tight deadline.
I'm trying the simplest thing, easily to achieve in SQL in a few seconds, and after half an hour with the ORM, I just can't get it to work.
1 event has 1 place. In EventModel:
public function place(){
$this->hasOne('zwoop\models\place\PlaceModel', 'place_id');
}
1 place can belong to many events. In PlaceModel:
public function events(){
$this->belongsToMany('zwoop\models\event\EventModel');
}
Now in the EventModel, what is the structure to get all events where place is within certain bounds? (eg. where place.lat > input_lat and lng > input_lng
(simplified).
I would give example code, but I can't figure it out at all.
This is just the beginning of a query that is really long, I really wonder if the ORM will be the most efficient solution in the end...
Upvotes: 1
Views: 152
Reputation: 649
Hmm. let me explain here a little bit. There are quite several ways to do it. But the most efficient way should be the following:
$places = Place::where('lat', '>', input_lat)->where('lng', '>', input_lng)->get();
$places = $places->lists('id');
Events::whereIn('place_id', $places)->get();
It is done!
Of course you can combine the two and make it one query. Personally I do not like mix queries thought it should give you a little boost in performance in your case.
Upvotes: 0
Reputation: 7879
Have you looked at the Querying Relations section of the Laravel Docs?
EventModel::whereHas('place', function($q) {
$q->where('lat', '>', input_lat)
->where('lng', '>', input_lng);
})->get();
Upvotes: 1