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

Andreas94's avatar

How to group by desc order with laravel?

Hi, I need to group some data, unfortunately though, I always get the lowest id, while I would like the highest one.

ID | id_topic
1   |    1
2  |    1
3  |    2

In this example, laravel prints to me:

ID | id_topic
3  |  2
1   |  1

while I would like:

ID | id_topic
3  |  2
2  |  1

My query is:

$topic = Forum_topic::with('lastpost','autore')->where('id_sezione', $sezione->id)->groupby('id_topic')->orderby('ID', 'DESC')->get();

Unfortunately, however, it seems that "orderby" does not work...

0 likes
7 replies
JackJones's avatar

In your example I don't see why it wouldn't retrieve all of the records

Andreas94's avatar

What do you mean @JackJones ? I was wrong to write the query, I corrected it, I had forgotten "groupby"

JackJones's avatar

I see now you've added groupBy(), if you are only trying to retrieve the ID, you can use MAX(ID)

Andreas94's avatar

Thanks for the answer, I tried in two ways, this is the first:

$topic = Forum_topic::with('lastpost','autore')->where('id_sezione', $sezione->id)->groupby('id_topic')->orderby('ID', 'DESC')->max('id')->get();

but I get the error Call to a member function get() on integer, while this:

$topic = Forum_topic::with('lastpost','autore')->where('id_sezione', $sezione->id)->groupby('id_topic')->orderby('ID', 'DESC')->max('id');

it works, but it only prints the highest id:

29 (which is the highest id I have in db)

Andreas94's avatar

I tried with the "WhereRaw", now I'm printing an array correctly

$topic = Forum_post::where('id_sezione', $sezione->id)->groupby('id_topic')->whereRaw('id = (select max(`id`) from forum_post)')->get();

Unfortunately, however, as you can see from the image, it only prints the first result, while I should print those highlighted in red...

https://i.imgur.com/IbtgOKF.png

rizwanrn's avatar

Just one answer, don't mess with groupBy() and unique() Answer: its for Inbox which shows latest msg for perticular user... My Case

$data = ChatMessage::where('incoming_msg_id', auth()->user()->id)->orWhere('outgoing_msg_id', auth()->user()->id)->latest('msg_id')->with('messageable')->get();
    $inbox = [];
    $itemIds = [];
    foreach ($data as $key => $value) {
        if (!in_array($value['messageable_id'], $itemIds)) {
            array_push($inbox,$value);
            array_push($itemIds,$value['messageable_id']);
        }
    }
    return $this->sendResponse($inbox,'Inbox');
Rooligan's avatar

You can solve your problem by using a subquery join. https://laravel.com/docs/8.x/queries#subquery-joins

$latestPosts = DB::table('forum_posts')
	->select(DB::raw('MAX(id) as id'))
	->groupBy('id_topic');

$posts = DB::table('forum_posts')
	->joinSub($latestPosts, 'latest_posts', function($join){
		$join->on('forum_posts.id', '=', 'latest_posts.id');
	})->where('id_sezione', $sezione->id)->get();

An other posibility is using whereIn with a subquery.

$posts = forum_posts::whereIn( 'id', function($query){
	$query->selectRaw('MAX(id)')->from('forum_posts')->groupBy('id_topic');
})->where('id_sezione', $sezione->id)->get();

I don't have enough posts in the database to see if there is a performance difference.

Please or to participate in this conversation.