php - Laravel with eager loading using condition -
i'm trying optimize relationship using eager loading , implement conditions in clause eager loading, when below:
$totalopenqry = enquiry::wherein('staff_id', $employeeids) ->with(['enquirystats' => function ($query) { $query->where('is_open','=',true) ->where('is_dead','=',false) ->orderby('id','asc'); }]) ->where('ed_timestamp', '>=', $daterange['start']) ->with('country'); $totalopen = $totalopenqry->tosql();
$totalopenqry->tosql()
produces following:
'select * `enquiries` `staff_id` in (10, 15) , `ed_timestamp` >= '2017-09-12';
it seems ignoring conditionals in clause. there way fix or implement properly?
thanks
wherehas()
works same has()
allows specify additional filters related model check.
$totalopenqry = enquiry::wherein('staff_id', $employeeids) ->with('enquirystats') ->wherehas('enquirystats', function ($query) { $query->where('is_open','=',true) ->where('is_dead','=',false) ->orderby('id','asc'); }) ->where('ed_timestamp', '>=', $daterange['start']) ->with('country'); // enquirystats have is_open = true , is_dead = false returned
Comments
Post a Comment