Попробуйте добавить область или два к вашей модели.
// User.php
protected $with = ['trips'];
public function trips() {
return $this->belongstoMany(Trip::class);
}
// scope for age
public function scopeAgeBetween($query, $age1, $age2) {
return $query->whereBetween('age', [$age1, $age2]);
}
// scope for trip dates
public function scopeTripDatesBetween($query, $dateStart, $dateEnd) {
return $query->whereDate('trips.start', '>=', $dateStart)
->whereDate('trips.end', '<=', $dateEnd);
}
Затем в вашем коде:
User::with('trips')->ageBetween(20, 30)->tripDatesBetween('2020-01-01', '2020-02-01')->get();