|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Database\Concerns; |
| 4 | + |
| 5 | +use Carbon\Carbon; |
| 6 | +use Illuminate\Support\Arr; |
| 7 | + |
| 8 | +trait BuildsWhereDateClauses |
| 9 | +{ |
| 10 | + /** |
| 11 | + * Add a where clause to determine if a "date" column is in the past to the query. |
| 12 | + * |
| 13 | + * @param array|string $columns |
| 14 | + * @return $this |
| 15 | + */ |
| 16 | + public function wherePast($columns) |
| 17 | + { |
| 18 | + return $this->wherePastOrFuture($columns, '<', 'and'); |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Add a where clause to determine if a "date" column is in the past or now to the query. |
| 23 | + * |
| 24 | + * @param array|string $columns |
| 25 | + * @return $this |
| 26 | + */ |
| 27 | + public function whereNowOrPast($columns) |
| 28 | + { |
| 29 | + return $this->wherePastOrFuture($columns, '<=', 'and'); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Add an "or where" clause to determine if a "date" column is in the past to the query. |
| 34 | + * |
| 35 | + * @param array|string $columns |
| 36 | + * @return $this |
| 37 | + */ |
| 38 | + public function orWherePast($columns) |
| 39 | + { |
| 40 | + return $this->wherePastOrFuture($columns, '<', 'or'); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Add a where clause to determine if a "date" column is in the past or now to the query. |
| 45 | + * |
| 46 | + * @param array|string $columns |
| 47 | + * @return $this |
| 48 | + */ |
| 49 | + public function orWhereNowOrPast($columns) |
| 50 | + { |
| 51 | + return $this->wherePastOrFuture($columns, '<=', 'or'); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Add a where clause to determine if a "date" column is in the future to the query. |
| 56 | + * |
| 57 | + * @param array|string $columns |
| 58 | + * @return $this |
| 59 | + */ |
| 60 | + public function whereFuture($columns) |
| 61 | + { |
| 62 | + return $this->wherePastOrFuture($columns, '>', 'and'); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Add a where clause to determine if a "date" column is in the future or now to the query. |
| 67 | + * |
| 68 | + * @param array|string $columns |
| 69 | + * @return $this |
| 70 | + */ |
| 71 | + public function whereNowOrFuture($columns) |
| 72 | + { |
| 73 | + return $this->wherePastOrFuture($columns, '>=', 'and'); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Add an "or where" clause to determine if a "date" column is in the future to the query. |
| 78 | + * |
| 79 | + * @param array|string $columns |
| 80 | + * @return $this |
| 81 | + */ |
| 82 | + public function orWhereFuture($columns) |
| 83 | + { |
| 84 | + return $this->wherePastOrFuture($columns, '>', 'or'); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Add an "or where" clause to determine if a "date" column is in the future or now to the query. |
| 89 | + * |
| 90 | + * @param array|string $columns |
| 91 | + * @return $this |
| 92 | + */ |
| 93 | + public function orWhereNowOrFuture($columns) |
| 94 | + { |
| 95 | + return $this->wherePastOrFuture($columns, '>=', 'or'); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Add an "where" clause to determine if a "date" column is in the past or future. |
| 100 | + * |
| 101 | + * @param array|string $columns |
| 102 | + * @return $this |
| 103 | + */ |
| 104 | + protected function wherePastOrFuture($columns, $operator, $boolean) |
| 105 | + { |
| 106 | + $type = 'Basic'; |
| 107 | + $value = Carbon::now(); |
| 108 | + |
| 109 | + foreach (Arr::wrap($columns) as $column) { |
| 110 | + $this->wheres[] = compact('type', 'column', 'boolean', 'operator', 'value'); |
| 111 | + |
| 112 | + $this->addBinding($value); |
| 113 | + } |
| 114 | + |
| 115 | + return $this; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Add a "where date" clause to determine if a "date" column is today to the query. |
| 120 | + * |
| 121 | + * @param array|string $columns |
| 122 | + * @param string $boolean |
| 123 | + * @return $this |
| 124 | + */ |
| 125 | + public function whereToday($columns, $boolean = 'and') |
| 126 | + { |
| 127 | + return $this->whereTodayBeforeOrAfter($columns, '=', $boolean); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Add a "where date" clause to determine if a "date" column is before today. |
| 132 | + * |
| 133 | + * @param array|string $columns |
| 134 | + * @return $this |
| 135 | + */ |
| 136 | + public function whereBeforeToday($columns) |
| 137 | + { |
| 138 | + return $this->whereTodayBeforeOrAfter($columns, '<', 'and'); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Add a "where date" clause to determine if a "date" column is today or before to the query. |
| 143 | + * |
| 144 | + * @param array|string $columns |
| 145 | + * @return $this |
| 146 | + */ |
| 147 | + public function whereTodayOrBefore($columns) |
| 148 | + { |
| 149 | + return $this->whereTodayBeforeOrAfter($columns, '<=', 'and'); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Add a "where date" clause to determine if a "date" column is after today. |
| 154 | + * |
| 155 | + * @param array|string $columns |
| 156 | + * @return $this |
| 157 | + */ |
| 158 | + public function whereAfterToday($columns) |
| 159 | + { |
| 160 | + return $this->whereTodayBeforeOrAfter($columns, '>', 'and'); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Add a "where date" clause to determine if a "date" column is today or after to the query. |
| 165 | + * |
| 166 | + * @param array|string $columns |
| 167 | + * @return $this |
| 168 | + */ |
| 169 | + public function whereTodayOrAfter($columns) |
| 170 | + { |
| 171 | + return $this->whereTodayBeforeOrAfter($columns, '>=', 'and'); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Add an "or where date" clause to determine if a "date" column is today to the query. |
| 176 | + * |
| 177 | + * @param array|string $columns |
| 178 | + * @return $this |
| 179 | + */ |
| 180 | + public function orWhereToday($columns) |
| 181 | + { |
| 182 | + return $this->whereToday($columns, 'or'); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Add an "or where date" clause to determine if a "date" column is before today. |
| 187 | + * |
| 188 | + * @param array|string $columns |
| 189 | + * @return $this |
| 190 | + */ |
| 191 | + public function orWhereBeforeToday($columns) |
| 192 | + { |
| 193 | + return $this->whereTodayBeforeOrAfter($columns, '<', 'or'); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Add an "or where date" clause to determine if a "date" column is today or before to the query. |
| 198 | + * |
| 199 | + * @param array|string $columns |
| 200 | + * @param string $boolean |
| 201 | + * @return $this |
| 202 | + */ |
| 203 | + public function orWhereTodayOrBefore($columns) |
| 204 | + { |
| 205 | + return $this->whereTodayBeforeOrAfter($columns, '<=', 'or'); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Add an "or where date" clause to determine if a "date" column is after today. |
| 210 | + * |
| 211 | + * @param array|string $columns |
| 212 | + * @param string $boolean |
| 213 | + * @return $this |
| 214 | + */ |
| 215 | + public function orWhereAfterToday($columns) |
| 216 | + { |
| 217 | + return $this->whereTodayBeforeOrAfter($columns, '>', 'or'); |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * Add an "or where date" clause to determine if a "date" column is today or after to the query. |
| 222 | + * |
| 223 | + * @param array|string $columns |
| 224 | + * @param string $boolean |
| 225 | + * @return $this |
| 226 | + */ |
| 227 | + public function orWhereTodayOrAfter($columns) |
| 228 | + { |
| 229 | + return $this->whereTodayBeforeOrAfter($columns, '>=', 'or'); |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * Add a "where date" clause to determine if a "date" column is today or after to the query. |
| 234 | + * |
| 235 | + * @param array|string $columns |
| 236 | + * @param string $operator |
| 237 | + * @param string $boolean |
| 238 | + * @return $this |
| 239 | + */ |
| 240 | + protected function whereTodayBeforeOrAfter($columns, $operator, $boolean) |
| 241 | + { |
| 242 | + $value = Carbon::today()->format('Y-m-d'); |
| 243 | + |
| 244 | + foreach (Arr::wrap($columns) as $column) { |
| 245 | + $this->addDateBasedWhere('Date', $column, $operator, $value, $boolean); |
| 246 | + } |
| 247 | + |
| 248 | + return $this; |
| 249 | + } |
| 250 | +} |
0 commit comments