How to make two WHERE clauses inside a nested WITH

2

I have the following code snippet

$sectors = Sector::where('company_id', $id)
                 ->where('status', '0')
                 ->with('charges.levels')
                 ->get();

I need 3 conditions

  • sector status is 0
  • charges status is 0
  • Status of levels is 0

So I would like to know:

  • How do I use a WHERE that looks for the status of charge 0 and the status of levels 0 being that they are in nested with : ->with('charges.levels')

The code follows the hierarchy that:

  • A sector has one or more positions and a position belongs to one sector only.
  • A position has one or more levels and a level belongs to a position only.

Taking into account that I want to bring all levels where the level is status 0, position status 0 and sector status 0

    
asked by anonymous 19.09.2018 / 16:04

2 answers

0

You can use past as an array:

$query->where([
    ['column_1', '=', 'value_1'],
    ['column_2', '<>', 'value_2'],
    [COLUMN, OPERATOR, VALUE],
    ...
])

Conditions using Array:

$users = User::where([
       'column1' => value1,
       'column2' => value2,
       'column3' => value3
])->get();

Will produce query as below:

SELECT * FROM TABLE WHERE column1=value1 and column2=value2 and column3=value3
    
19.09.2018 / 17:14
0

I believe that what you are looking for is something similar to this, try the following:

$sector = Sector::whereHas('charges', function ($query) use ($search) {
         $query->where('levels', $search);
    })->with('charges.levels')->where('company_id', $id)->get();

I hope I have helped.

    
19.09.2018 / 20:31