All individual subscriptions are 50% off this week only!

MarkyzZz
7 months ago

Any ideas how to extract certain fields after a groupBy() on a collection?

Posted 7 months ago by MarkyzZz
$gpsdata = Gpsdata::select(['scooter_id', 'lat', 'long'])
->whereIn('scooter_id', $scooter_ids);
->get()
 ->groupBy('scooter_id');

All good, but i don't have any idea how to extract only lat, long fields after groupBy().

Example output:

Illuminate\Database\Eloquent\Collection {#1099
     all: [
       34 => Illuminate\Database\Eloquent\Collection {#1053
         all: [
           App\Models\Gpsdata {#1071
             scooter_id: 34,
             lat: "47.057042",
             long: "28.885650",
           },   
        App\Models\Gpsdata {#1142  
        scooter_id: 34,          
        lat: "47.052042",        
        long: "28.893650",       
          },                                              
     ],
       },
       35 => Illuminate\Database\Eloquent\Collection {#1054
         all: [
           App\Models\Gpsdata {#1082
             scooter_id: 35,
             lat: "47.051042",
             long: "28.861650",
           },
      App\Models\Gpsdata {#1081
             scooter_id: 35,        
             lat: "47.076042",      
             long: "28.863650",     
      },     
    ]
      }
    ]
  }     

Please sign in or create an account to participate in this conversation.