Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kaju74's avatar

Get latest records ordered by date?

Hi, I've a problem getting the latest records of a tables ordered by another column.

So, here are some demo records:

"user_id";"start_at";"end_at";"target";"actual";"delta";"balance";"data";"created_at"
"113";"2016-05-02 00:00:00";"2016-05-08 23:59:59";"39,5";"39,5";"0";"0";"data";"2016-05-04 12:25:11"
"113";"2016-05-02 00:00:00";"2016-05-08 23:59:59";"39,5";"39,5";"0";"0";"data";"2016-05-04 12:23:32"
"113";"2016-05-09 00:00:00";"2016-05-15 23:59:59";"39,5";"39,5";"0";"0";"data";"2016-05-09 12:58:01"
"113";"2016-05-09 00:00:00";"2016-05-15 23:59:59";"39,5";"39,5";"0";"0";"data";"2016-05-04 12:25:11"
"113";"2016-05-09 00:00:00";"2016-05-15 23:59:59";"39,5";"39,5";"0";"0";"data";2016-05-04 12:23:32"

I want to get only the latest created records (created_at) from every available week (start_at). As you can see, there are two records for the week from 2016-05-02 and three records for 2016-05-09. The final result should be:

"user_id";"start_at";"end_at";"target";"actual";"delta";"balance";"data";"created_at"
"113";"2016-05-02 00:00:00";"2016-05-08 23:59:59";"39,5";"39,5";"0";"0";"data";"2016-05-04 12:25:11"
"113";"2016-05-09 00:00:00";"2016-05-15 23:59:59";"39,5";"39,5";"0";"0";"data";"2016-05-09 12:58:01"

How do I write the eloquent query for this? I've tryed several things, but nothing works 8-(

Thank you very much, Marc

P.S.: I know, one 'created_at' date is in the future ;-)

0 likes
7 replies
kaju74's avatar

Hi,

thank you for the quick answer, but this only calls a simple "$this->orderBy($column, 'desc')" and doesn't return the latest row of EACH week...

Marc

kaju74's avatar

Okay, it seems, I found a solution:

        $publications = $query
            ->where('user_id', $user->id)
            ->where('start_at', '>=', Date::now()->startOfWeek())
            ->orderBy('start_at', 'asc')
            ->orderBy('created_at', 'desc')
            ->get()
            ->groupBy(function ($item) {
                return substr($val = $item['start_at'], 0, strpos($val, ' '));
            })
            ->pluck(0);

...but, is there's a smarter way to do this? Marc

d3xt3r's avatar

@kaju74 The solution you posted will load all the results and group inside the php function, will work but not very elegant if you have many rows to return.

Another solution involves using group by inside the sql query. You will have to create subtable using aggregate functions and group by, join it, to original table and then select from them.

kaju74's avatar

...that's what I've tryed but I can't figured out the correct syntax...8-(

Regards, Marc

ricardoarg's avatar

you need to do this query:

SELECT t1.* FROM table as t1 WHERE (t1.start_at, t1.created_at) IN (SELECT t2.start_at, MAX(t2.created_at) FROM table as t2 GROUP BY t2.start_at);

(you need an index on start_at/created_at)

just use DB::select(), or somebody with more knowledge can say how to use the subquery with eloquent / query builder.

1 like

Please or to participate in this conversation.