Equivalent to MySQL YEAR () in Laravel Query Builder

In MySQL, I can use the YEAR() function to filter by year the date field in the WHERE

 SELECT noworkorder FROM workorders WHERE YEAR(date)=2015; 

In Laravel, of course, I can achieve the same as raw expression :

 $data = DB::table('workorders') ->select('noworkorder') ->where(DB::raw('YEAR(date)=2015')) ->orderby('noworkorder', 'desc') ->get(); 

But is there a way to do this without raw expressions?

+5
source share
1 answer

The query designer has a whereYear method:

 $data = DB::table('workorders') ->select('noworkorder') ->whereYear('date', '=', 2015) ->orderby('noworkorder', 'desc') ->get(); 
+10
source

Source: https://habr.com/ru/post/1235257/


All Articles