Laravel Eloquent Query – Laravel Group By with Order By Desc | CodeSolutionStuff – Web Development Websites, Tutorials & Solutions

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.

Alternate Text Gọi ngay