Answers for "groupby in laravel"

PHP
4

group by laravel

$user_info = DB::table('usermetas')
                 ->select('browser', DB::raw('count(*) as total'))
                 ->groupBy('browser')
                 ->get();
Posted by: Guest on August-06-2020
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);
}
Posted by: Guest on December-21-2020
0

laravel groupby and latest

//If You want the latest id of records then you can use unique() after get(),
//don't use group by if you use groupBy 
//then you lose your control from id. I hope this is useful for you

myModel::select('id','purch','name','prcvalue')
  ->where('purch','=','10234')
  ->orderBy('prcvalue','DESC')
  ->get()
  ->unique('name');
Posted by: Guest on June-14-2021

Browse Popular Code Answers by Language