Suppose there is a table of users who have two fields: month and year of birth. You need to make a selection: show users in the range of December 2017 to January 2018.
For example: $ users = User :: query () -> where ('month', '> =', 12) -> where ('year', '> =', 2017) -> where ('month', ' <= ', 1) -> where (' year ',' <= ', 2018) -> get (); but it is not clear how to combine, so that the conditions> = 12 and> = 2017 are fulfilled simultaneously.
Those. first found the records that are included in the condition> = 12 and> = 2017, and then <= 1 and <= 2018.

  • Convert to YYYYMM format YYYYMM , and then stupidly BETWEEN '201712' AND '201801' . Well, or by number, WHERE year*100+month BETWEEN 201712 AND 201801 (however, you can multiply by 12 ...). - Akina
  • @Akina only has my month and year in different fields, if you use whereBetween, then the first parameter is the field, and then the range, how in this case to be? - Ivan
  • WHERE (поле или выражение) BETWEEN (нижняя граница) AND (верхняя граница) In this case, an expression will be used that is calculated based on the values ​​of two different fields - everything is exactly. - Akina
  • one
    Because 201712 is not a field name, but a kind of literal (value). And instead there should be a evaluating expression. And, accordingly, DB :: Raw. - Akina
  • one
    And with the help of where you can do it so that it is checked not by one field, but two at once? Yes, at least ten. I did not try to watch the documentation? laravel.com/docs/5.6/queries#where-clauses , section Parameter Grouping. - Akina

1 answer 1

As a solution to the problem, you can use whereRaw: https://laravel.com/docs/5.6/queries#raw-expressions