дело решилось запросом посгрес действительно
$sql = '
WITH RECURSIVE temp1 ( id,id_parent,name,PATH, LEVEL, NAME ) AS (
SELECT T1.id,T1.id_parent, T1.name, CAST (T1.id AS VARCHAR (50)) as PATH, 1 ,
CAST (T1.name AS VARCHAR (255)) as NAME
FROM vgok_site.t_department T1 WHERE T1.id_parent = 0
union
select T2.id, T2.id_parent, T2.name, CAST ( temp1.PATH ||\'->\'|| T2.id AS VARCHAR(50)) ,LEVEL + 1 ,
CAST ((repeat(\' _ \', LEVEL+1)||T2.name) AS VARCHAR(255))
FROM vgok_site.t_department T2 INNER JOIN temp1 ON( temp1.id= T2.id_parent))
select * from temp1 ORDER BY PATH LIMIT 100
';