I have the following 2 tables ,
users                    keys
 id   name                id    key   customerid      date
 ___________             ____________________________________
  1    1name               1     1        1        2016-11-17
  2    2name               2     1        1        2016-11-19      
                           3     2        1        2016-11-18
                           4     1        1        2016-11-18
I want to get for every customer the ids of each key having the max date
Customers::with(['keys => function($query){
    $query->groupBy('key')
          ->select('id',DB::raw('max(date)'));
}])
->get();
Now I'm getting the max date and the id of the first row not the id of the record having the max date
