The database query builder in Laravel provides a simple, intuitive interface for generating and performing database queries. It works with every one of Laravel's supported database systems and may be used to conduct most operations on the database in your application.

This page is about the Laravel group, and it's organized by desc. You've come to the right place if you're looking for an example of a laravel eloquent group by order by desc. By ordering by, you can grasp the concept of the Laravel group. I'd want to show you how to use the laravel eloquent group by and order by functions. It will also work with the Laravel application.
In this post, I'll show you how to get entries with an order by desc with the group by in a Laravel application. When using laravel eloquent group by with order by desc, it now works as expected.

Solution 1

/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
 $messages = Message::select("*")
			->where('receiver_id',$id)
			->orderBy('created_at', 'desc')
			->get()
			->unique('sender_id');
			dd($messages);
}

Solution 2

/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
	$message = Message::orderBy('created_at','DESC');
	$messages = DB::table(DB::raw("({$message->toSql()}) as sub"))
			->where('receiver_id',$id)
			->groupBy('sender_id')
			->get();
			dd($messages);
}

Solution 3

/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
	$messges = Message::select(DB::raw('*, max(created_at) as created_at'))
			->where('receiver_id',$id)
			->orderBy('created_at', 'desc')
			->groupBy('sender_id')
			->get();
			dd($messages);
}

 

Solution 4

/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
	$messages = Message::select("*")
				->where('receiver_id',$id)
				->orderBy('created_at', 'desc')
				->groupBy('sender_id')
				->get();
				dd($messages);
}
 

I hope you will like the content and it will help you to learn Laravel Eloquent Query - Laravel Group By with Order By Desc
If you like this content, do share.


Recommended Posts

View All

Laravel 8 Collection with first() and firstWhere() Methods Example


You'll learn how to use the first() and firstWhere() methods in Laravel 8 Collection. The Laravel collection will make working with arrays much easier

Social Media Share Buttons Integration Tutorial in Laravel 9


This fast guide will demonstrate how to use the laravel-share package to add social media share buttons to each page of Laravel.

Laravel 9 Livewire Form Submit Tutorial


Laravel 9 Work With Livewire Form Submit Tutorial. laravel 9 livewire form submit example, create form with laravel 9 livewire, laravel 9 livewire tut...

Laravel whereBetween Query Example


In this post, we will see Laravel whereBetween query model. SQL gives numerous different sorts of techniques or queries to get separated information f...

Laravel 9 Dropzone Image Upload Example Step By Step


The most well-known, free, and open-source library for drag-and-drop file uploads with image previews is Dropzone. I'll be using Laravel 9 in...