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.
|
1 answer
As a solution to the problem, you can use whereRaw: https://laravel.com/docs/5.6/queries#raw-expressions
|
YYYYMMformatYYYYMM, and then stupidlyBETWEEN '201712' AND '201801'. Well, or by number,WHERE year*100+month BETWEEN 201712 AND 201801(however, you can multiply by 12 ...). - AkinaWHERE (поле или выражение) BETWEEN (нижняя граница) AND (верхняя граница)In this case, an expression will be used that is calculated based on the values of two different fields - everything is exactly. - Akina201712is not a field name, but a kind of literal (value). And instead there should be a evaluating expression. And, accordingly, DB :: Raw. - Akina