How to get all records for the current year?

How to get all the articles for the current year?

Articles::all()->where('created_at',$current_year);
+4
source share
2 answers

You can do it:

Articles::whereBetween('created_at', [
    Carbon::now()->startOfYear(),
    Carbon::now()->endOfYear(),
]);

Another way is to use whereYear()as in @xhulio's answer. But I would recommend you use this code as it is more readable:

Articles::whereYear('created_at', date('Y'))->get();
+6
source

Try the following query

Articles::whereYear('created_at', '=', Carbon::parse($date)->format('Y'))->get();

Laravel has auxiliary functions whereDate, whereDay, whereMonthand whereYearto cope better with the demands of datetime.

+1
source

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


All Articles