0

I am working on api, and using resource to send data. I want to send collection which is grouped by status. So far I have done the following:

public function data(){
    $collection = ModelNameResource::collection(
        ModelName::query()
            ->latest()
            ->get()
    );
    $data = $collection->groupBy('status');
    return $data;
}

Although I have got what I want but want to know if there is the better approach to achieve this?

Gaurav
  • 149
  • 12
  • 2
    Not sure what you are asking here. Better approach in what respect? Space complexity? Time complexity? Lines of code? Cleanliness of code? – apokryfos Dec 08 '21 at 07:05

1 Answers1

0

if you mean clean code

public function data(){
    return ModelNameResource::collection(
        ModelName::query()
            ->latest()
            ->get()
    )->groupBy('status');
}
Ayoub ait
  • 163
  • 2
  • 4