php - Get latest distinct value of a filed in Laravel -


i want query latest + distinct name.

i got distinct part work, they're not latest. i'm not sure how in laravel.


i’ve tried

$localdevices = device::orderby('created_at', 'desc')->groupby('mac')->get();          $localdevicename = [];         $i = 0;          foreach ($localdevices $localdevice) {             foreach ($devices $device) {                 if($localdevice->mac == $device->device_mac ){                     $localdevicename[$i]['name'] = $localdevice->name;                     $localdevicename[$i]['mac'] = $device->device_mac;                     $i++;                 }             }         } 

database

enter image description here


i got

array:1 [▼   0 => array:3 [▼     "name" => "apple watch"     "img" => "/images/photos/devices/apple-watch.jpg"     "mac" => "080027e2fc7d"   ] ] 

i want show ps4 because latest.


try #2

tried update my

orderby('created_at', 'desc') orderby('created_at', 'asc')

i got same result.


try #3

tried placing orderby after groupby

device::groupby('mac')->orderby('created_at', 'desc')->get();

i got same result.


any hints / suggestions on appreciated !

you doing groupby on mac value isn't unique, apple watch , ps4 have same mac, mysql first groups orders grouped results. that's why getting apple watch.

what want fetch latest record each group , might write raw query, check retrieving last record in each group


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -