Question: How to get all records for current year?

Question

How to get all records for current year?

Answers 2
Added at 2016-12-26 12:12
Tags
Question

How to get all articles for current year?

Articles::all()->where('created_at',$current_year);
Answers
nr: #1 dodano: 2016-12-26 12:12

You can do this:

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

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

Articles::whereYear('created_at', date('Y'))->get();
nr: #2 dodano: 2016-12-26 12:12

Try the following query

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

Laravel has the helper functions whereDate, whereDay, whereMonth and whereYear to better deal with datetime queries.

Source Show
◀ Wstecz