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 Generate a Variety of QR Codes in a Laravel 9 App


In the Laravel application, a basic QR code generator allows you to generate several sorts of QR Codes.

How to Install Ckeditor in Laravel 9


Laravel 9 ckeditor tutorial example, you will learn how to install or integrate ckeditor and use in laravel 9 app

Integrate Summernote Tutorial Example in Laravel 9


laravel summernote editor example, how to use summernote editor in laravel, how to install summernote in laravel, summernote editor in laravel, larave...

How to Create REST API in Laravel7 using Passport


rest api in laravel 7/6 step by step, rest api in laravel 7/6 tutorial, laravel 7/6 restful api authentication, laravel 7/6 passport rest api tutorial

Building Smarter Web Applications with OpenAI for Laravel


In this tutorial, we'll explore how to use OpenAI in Laravel, and how it can help us build smarter, more efficient web applications.