orderBy('id', 'asc')->get(); $collection = collect(); foreach ($devices as $key => $value) { $uniqueCommandsCount = Config::select('command')->where('device_id', $value->id)->distinct('command')->count(); $collection->push(Config::all()->where('device_id', $value->id)->sortByDesc('created_at')->take($uniqueCommandsCount)); } $flattenedCollection = $collection->flatten(); $dataOutput = collect(); foreach ($flattenedCollection as $key => $value) { $dataOutput->push($value->only('id', 'device_id', 'device_name', 'device_category', 'command', 'config_filesize', 'created_at')); } $headers = ['ID', 'DEVICE ID', 'DEVICE_NAME', 'CATEGORY', 'COMMAND', 'FILE SZIE', 'CREATED DATE']; $this->info('Results for Latest Configs List:'); $this->table($headers, $dataOutput); } }