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

wireshark - USB mapping with python -

c++ - nodejs socket.io closes connection before upgrading to websocket -

Deploying Qt Application on Android is really slow? -