laravel-sub-query
laravel-sub-query copied to clipboard
Wouldn't it be nicer to do these calculations in PHP rather than tiring the MySQL?
Wouldn't it be nicer to do these calculations in PHP rather than tiring the MySQL?
/**
* Find items for current year
* @param string $column
* @return $this
*/
public function whereCurrentYear($column = 'created_at')
{
$now = now()->toDateTimeString();
$startOfThisYear = Carbon::parse('first day of this year')->toDateTimeString();
return $this->whereRaw("$column between {$startOfThisYear} and {$now}");
}
https://github.com/Alexmg86/laravel-sub-query/blob/42e586fe10dc87643776c76961cbf1451d19a759/src/Traits/LaravelSubQuerySugar.php#L105-L113
SELECT * FROM `candidates` WHERE created_at BETWEEN date_format(now() ,'%Y-01-01') and now();
SELECT * FROM `candidates` WHERE created_at BETWEEN "2022-01-01 00:00:00" and "2022-09-28 23:59:59";