4

I`m having trouble to setup a negative condition like such:

WHERE NOT( "last_day" "<=" $first_day OR "first_day" "<=" $last_day)

My query builder looks like this atm:

$query = $query->where(function ($query) use ($first_day, $last_day) {
   $query->where('last_day', '<=', $first_day);
   $query->orWhere('first_day', '<=', $last_day);
});

I would like it to be as such :

$query = $query->whereNot(function ($query) use ($first_day, $last_day) {
  $query->where('last_day', '<=', $first_day);
  $query->orWhere('first_day', '<=', $last_day);
});

To recap: I want an OR statement inside a negative WHERE condition. How can I accomplish this?

source : http://baodad.blogspot.nl/2014/06/date-range-overlap.html

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
MrMedicine
  • 53
  • 12

2 Answers2

0

You can play with the logic

$query = $query->where(function ($query) use ($first_day, $last_day) {
   $query->where('last_day', '>', $first_day);
   $query->where('first_day', '>', $last_day);
});
  • I don`t think the logic is the same, it does not qualify: "Allen's interval algebra" : https://en.wikipedia.org/wiki/Allen%27s_interval_algebra – MrMedicine Apr 04 '17 at 18:54
  • Change : $query->where('first_day', '>', $last_day); to $query->where('first_day', '<', $last_day); and its correct, see : http://stackoverflow.com/questions/43233652/eloquent-mysql-statement-where-nota-or-b – MrMedicine Apr 05 '17 at 15:23
0

Went with reverting constraints according to answer found here: Eloquent MYSQL statement : WHERE NOT(A OR B)

$query = $query->where('last_day', '>', $first_day)->where('first_day', '>', $last_day);
Community
  • 1
  • 1
MrMedicine
  • 53
  • 12