How to make two WHERE clauses inside a nested WITH

Asked

Viewed 459 times

3

I have the following code snippet

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

and need 3 conditions

  • Status of sector be it 0
  • Status of cartoons be it 0
  • Status of levels be it 0

So I’d like to know:

  • How do I use a WHERE that searches for Charge 0 status and level 0 status being that they are in a 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 post has one or more levels and a level belongs to one post only.

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

  • I think this is what you want: https://laravel.com/docs/5.5/eloquent-relationships#constraining-Eager-loads

  • I can not comment yet so I will comment here, could post your models, Migrations if use and appropriate relationships for analysis?

  • Which version of the Laravel you are using?

3 answers

1

0

I believe that what you seek 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’ve helped.

0

you can use passed as 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
  • But the problem is that the status of cartoons is in the cartoons table, and the status of the levels is in the levels table, I had done so, but when I put Where('charges.status', '0'), it turned out that cartoons.status was not a column

Browser other questions tagged

You are not signed in. Login or sign up in order to post.