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

pakistanihaider's avatar

Get result through laravel eloquent based on Last date or max number

normally to fetch latest record i use desc order and use limit, but now i want multiple records for the latest date or max number, So if a column has maxiumum number e-g 3, and there are multiple records with this max number, so i want to fetch all those records with this max number.

i tried using max(), but it returns me the 3 integer number not the records its self.

Any idea how i can fetch records for latest number or maximum number in database using laravel eloquent?

0 likes
3 replies
Snapey's avatar

perhaps 2 queries might be simplest.

first get your max, eg 3, and then in a separate query, get all records where the number is 3

pakistanihaider's avatar

@Snapey You are right, but i wanted to do it in single query or kind of sub query in eloquent.

$offer->offerPageContents()->revision()->where(function($offerContent){
            $offerContent->where('revision_id',$offerContent->max('revision_id'));
        })->get();

This seems to be working, but i am not sure if its efficient.

Snapey's avatar

I would care more that revision_id is indexed for faster searching.

Install debugbar and you will see what queries are performed and how long each takes

Please or to participate in this conversation.