Laravel agroup data by idParent

  laravel, php

I use laravel to make a api and now I need help to agroup records in my table companies.

The structure of the companies table is:

id | idParent | companyName
1  | null     | Company A
2  | 1        | Company B
3  | null     | Company C
4  | 3        | Company D
5  | 1        | Company E
6  | 3        | Company F

How can I get the data agroup by idParent like this:

id | idParent | companyName
1  | null     | Company A
2  | 1        | Company B
5  | 1        | Company E
3  | null     | Company C
4  | 3        | Company D
6  | 3        | Company F

Any help will be welcome.

Thanks!!

Source: Ask PHP

LEAVE A COMMENT