morne
morne

Reputation: 4189

Laravel conditional eloquent query

I have this query.

return $this->model->with( [ 'items' => function($query){
  $query->where('invisible','=',FALSE)->orderBy('description', 'ASC');
}])
->where("org_id",$org_id)
->where('invisible','=',FALSE)
->orderBy('description', 'asc')->get();

I would like to find out if its possible to check if any records where returned from the items model. If no records where returned, that it would not append the Group to the result

At the moment Im getting some Group records containing no items.

Hope that make sense.

Upvotes: 0

Views: 64

Answers (1)

Chamara Abeysekara
Chamara Abeysekara

Reputation: 1332

You can use whereHas for this. try this out,

return $this->model->with('items')
->whereHas('items',fuction ($query){
   $query->where('invisible','=',FALSE)->orderBy('description', 'ASC');
})
->where("org_id",$org_id)
->where('invisible','=',FALSE)
->orderBy('description', 'asc')->get();

Hope this helps

Upvotes: 1

Related Questions