deepu07
8 months ago

Laravel API groupBy format

Posted 8 months ago by deepu07

Hi Guys this my logic. In result I want to display all the records groupBy subject_id. (In input user can pass multiple subjectIds). with this logic im not getting results in groupBy format. How can I do that? any help that would be appreciated. Thanks


        $data = [];
   
        foreach ($subjects as $subjectId) {
            $student = Student::select(['*'])
                                ->where('subject_id', $subjectId)
                                ->orderBy('grade')
                                ->get()
                                ->toArray();
                                
            array_push($data, $student);
        }
        return response(['success' => true, 'data' => $data]);```

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