laravel - Ordering by updated_at per group -


i have following simple query attempt last updated row per module:

    $distincts = serialnumber::where('company_id', auth::user()->active_company_id)         ->groupby('module')         ->orderby('updated_at', 'desc')         ->get(); 

this doesn't work way want to. each row in module group not ordered updated_at (which datetime stamp).

how can use orderby each row in group? reading!

the following works, bad solution:

    $distincts = array();      $modules = serialnumber::select('module')         ->where('company_id', auth::user()->active_company_id)         ->groupby('module')         ->pluck('module');      foreach ($modules $mod) {         $se = serialnumber::where('company_id', auth::user()->active_company_id)             ->where('module', $mod)             ->orderby('updated_at', 'desc')             ->first();          $distincts[] = $se;     } 

what need subquery:

$query = serialnumber::orderby('updated_at', 'desc')->getquery();  $distincts = db::table(db::raw("(". $query->tosql() . ") subquery"))     ->where('company_id', auth::user()->active_company_id)     ->groupby('module')     ->get(); 

this makes latest records in group.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -