laravel group by count code example

Example 1: groupby in laravel with count

$user_info = Usermeta::groupBy('browser')->select('browser', DB::raw('count(*) as total'))->get();

Example 2: 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 3: count column eloquent laravel

$total = Post::sum('views');

Tags:

Php Example