I have an eloquent query:
$cameras=Jump::Video()
->with('flight','flight.aircraft','flight.airfield')
->with('student','student.logininfo','student.logininfo.reservations')
->with('camera','camera.status')
->whereBetween('data',[ $from, $to])
->orderBy($sortBy, $orderDesc)
->paginate($request->perPage);
and relations between student, logininfo and reservations
Model Jump.php
public function scopeVideo($query)
{
return $query->where('exercise', '104');
}
public function student() {
return $this->hasOne('AppModelsPersonalData','id','student');
}
Model PersonalData.php
public function logininfo() {
return $this->hasOne('AppUser','id_user','id');
}
Model User.php
public function reservations() {
return $this->hasMany('AppModelsReservation','user_id', 'id');
}
I’d like to get only Jumps where ‘type’ in table ‘reservation’ (model Reservation) is equal $request->type
I was trying:
->where('student.logininfo.reservations','=',$request->type)
OR
->whereHas('student.logininfo.reservations',function($query) use ($request) {
return $query->where('type','=',$request->type)
})
and it didn’t work. Any suggestions?
2
Answers
Thank you for your post. It's a really helpful. Unfortunately withWhereHas is killing my server. I don't know why but there is no response from server (connection timed out). It looks like overloaded. I can't even stop listening (php artisan serve) and I need to kill php (laravel) process.
I was using flight, flight.aircraft etc because it was simplification for this case and in my real code I use specific fields to limit size of downloaded objects. My real code looks like that:
I changed this code to:
And my server (laravel) is not responding anymore :( I have to kill the php process and restart server to bring it back to live.
whereHas is killing my server as well:
Couple things, you don’t need multiple
->with()
clauses, nor do you need to chain the same Table multiple times. You can reduce your->with()->with()->with()
to simply:Dot-notation like
student.logininfo.reservations
will load thestudents,
logininfo
andreservations
Relationships, so you don’t need->with('a', 'a.b', 'a.b.c')
, only->with('a.b.c')
.Next, if you only want to load
reservations
of a specific type, you can append a function to yourwith()
:If you need to limit the
Jump
models to only those that have a specificreservations.type
, use->whereHas()
:If you need filter AND limit, then you use
->withWhereHas()
:So, putting it all together:
Adjust as needed, and reference the documentation if you get stuck:
https://laravel.com/docs/11.x/eloquent-relationships#querying-relations
https://laravel.com/docs/11.x/eloquent-relationships#querying-relationship-existence