Where clause with nested AND/OR

Kumidan posted 3 years ago

I need to write a where clause with Query Builder with nested AND/OR, I suppose that I have to use whereNested() but I have not understood how to do.

An example of the where clause is

 WHERE ( ( ( date_start_month < 5 AND date_start_year = 2015 ) OR date_start_year < 2015 ) AND date_end_month = 5 AND date_end_year = 2015 )

how should I set this clause using QB?

sparksp replied 3 years ago Solution

You need to nest a function each time you want some brackets in your query, like this (untested)...

  ->where(function($startQuery) {
      ->where(function($thisYearQuery) {
          ->where('date_start_month', '<', 5)
          ->where('date_start_year', 2015);
      ->orWhere('date_start_year', '<', 2015);
  ->where('date_end_month', 5)
  ->where('date_end_year', 2015);
Kumidan replied 3 years ago

Thank you.

Sign in to participate in this thread!

We'd like to thank these amazing companies for supporting us