php - Adding parameters to the relation in laravel -
i have query uses many ->with()
in laravel so:
$campaign = $this->campaign ->with('tracks.flights.asset')->take(4) ->with('tracks.group') ->with('tracks.media') ->with('tracks.flights.comments') ->orderby('created_at', 'desc')->find($id);
and in track
model have method:
public function flights() { return $this->hasmany('flight\flight')->orderby('start_date'); }
what want achieve having same result want put date constrains on flight level, example this:
public function flights($start_date, $end_date) //dynamic dates { return $this->hasmany('flight\flight')->whereraw('start_date > $start_date , end_date < $end_date')->orderby('start_date'); }
how can achieve kind of result? new laravel.
thanks in advance.
chain wherehas
onto query.
$campaign = $this->campaign ->with('tracks.flights.asset')->take(4) ->with('tracks.group') ->with('tracks.media') ->with('tracks.flights.comments') ->wherehas('tracks.flights', function($q) use ($start_date, $end_date) { $q->where('start_date', '>', $start_date)->where('end_date', '<', $end_date)->orderby('start_date'); }) ->orderby('created_at', 'desc')->find($id);
keep in mind orderby
order results returned within flights relation, not campaigns themselves.
Comments
Post a Comment