S
S
Sergei Gurdjiyan2018-03-24 11:05:14
Laravel
Sergei Gurdjiyan, 2018-03-24 11:05:14

How to get all years of records in Laravel table?

There is a table of posts with a standard field created_at.
You need to get a list of all post publication years.
What is the best way to do this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergei Gurdjiyan, 2018-03-24
@mrKorg

$query->select(DB::raw( 'YEAR(created_at) as year' ))->distinct()

V
Vladislav Nagorny, 2018-03-24
@esvils

$query->groupBy(DB::raw('YEAR(created_at)'))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question