0

I'm making a query using the following line:

$items = $this->model->with('subCategory')->get();

But I want to put a query inside the with() method, because I just want to get the items from with() where the status is equal to 0.

How can I achieve this?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Caio Kawasaki
  • 2,894
  • 6
  • 33
  • 69

2 Answers2

2

There is an "eager loading" in L5 documentation. Here

$items = $this->model->with(['subCategory' => function ($query) {
  $query->where('status', 0); }])->get();
jsdecena
  • 572
  • 2
  • 7
  • 22
1

These are called eagarload constraints, you can achieve your result using a closure

For example

$items = $this->model->with(['subCategory'=>function($q){
    $q->whereId('5');
    //or any other valid query builder method.
}])->get();

Let me know how you get on.

ExohJosh
  • 1,832
  • 16
  • 22