has not working in relationship query

Posted 1 year ago by arctushar

My eloquent is as below

return $codelists=Codelist::select('id','code','category')->with(['dsedatalast'=>function($query)use($date,$alertvalue){
            $query->select('id','codelist_id','value','date','volume','ltp')->where('date',$date)->where('value','>',$alertvalue/10);
        }])->has('dsedatalast')->get(); 

Here some dsedatalast is null and some has value. I wanted to ignore where dsedatalast is null and need where dsedatalast has value

My correct output of above code as as below

[{"id":1,"code":"1JANATAMF","category":"A","dsedatalast":null},{"id":2,"code":"1STPRIMFMF","category":"A","dsedatalast":{"id":691653,"codelist_id":2,"value":"1.472","date":"2018-06-07","volume":116385,"ltp":"12.6"}},{"id":3,"code":"AAMRATECH","category":"A","dsedatalast":{"id":691655,"codelist_id":3,"value":"1.440","date":"2018-06-07","volume":51177,"ltp":"28.0"}},{"id":4,"code":"ABB1STMF","category":"A","dsedatalast":null},{"id":5,"code":"ABBANK","category":"Z","dsedatalast":{"id":691426,"codelist_id":5,"value":"5.079","date":"2018-06-07","volume":408603,"ltp":"12.5"}},{"id":6,"code":"ACI","category":"A","dsedatalast":{"id":691656,"codelist_id":6,"value":"13.540","date":"2018-06-07","volume":39686,"ltp":"341.4"}},{"id":7,"code":"ACIFORMULA","category":"A","dsedatalast":{"id":691657,"codelist_id":7,"value":"4.334","date":"2018-06-07","volume":26471,"ltp":"164.7"}]

I even used whereHas instead of, result is same. NB: I want only those codelists whre dsedatalast is not null.

Please sign in or create an account to participate in this conversation.