group by and count in laravel eloquent code example
Example 1: groupby in laravel
You can use unique('field_name'); instead of groupBy('field_name');
/**
* 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);
}
Example 2: count column eloquent laravel
$total = Post::sum('views');