Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

erhansogood's avatar

whereHas relationship getting the latest row, but want to do a where on this result

//model
    public function latest_flight_stock()
    {
        return $this->hasOne('App\FlightStock')->latest();
    }

//controller
  $flights = Flight::with('latest_flight_stock')
            ->has('flight_stocks')
            ->orderBy('departure_datetime');
            ->whereHas('latest_flight_stock', function($query) use($request) {
                $query->where('load', '>', 30);
            })
    ->get();

but now its fetching the load higher then 30 from Flight stock but what i like to accomplish is fetching the lateststocks with a load higher then 30.

0 likes
0 replies

Please or to participate in this conversation.