Alexmg86/laravel-sub-query

Wouldn't it be nicer to do these calculations in PHP rather than tiring the MySQL?

OzanKurt opened this issue · 0 comments

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}"); 
     } 

/**
* Find items for current year
* @param string $column
* @return $this
*/
public function whereCurrentYear($column = 'created_at')
{
return $this->whereRaw("$column between date_format(now() ,'%Y-01-01') and now()");
}

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";