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

How To Create Word Document File In Laravel


In Laravel, use the phpoffice/phpword package to create a word document file. In this example, I'll show you how to create a word document an...

Laravel 9 GEO Chart Example using LavaCharts


Learn how to create interactive GEO charts with Laravel 9 and LavaCharts. Visualize your data on maps and improve data analysis. Check it out now!

Laravel 8 Generate PDF with Graph Tutorial


Laravel 8 generates a graphed pdf. You will understand how to generate a pdf with a graph in the Laravel 8 app in this tutorial

PayPal Integration in Laravel Example


PayPal Integration in Laravel, laravel paypal integration, laravel setup paypal, laravel paypal integration example

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...