I have a table in the database with navbar menu items, this table has a relationship 1xN with itself, I'm retrieving the data (like: \App\Menu::with(['childs'])->get()
) as:
array(
array(
'id' => 1,
'id_parent' => NULL,
'url' => '/',
'childs' => array ()
),
array(
'id' => 2,
'id_parent' => NULL,
'url' => '/blog',
'childs' => array (
array(
'id' => 3,
'id_parent' => 2,
'url' => 'blog/kitchen',
'childs' => array(
array(
'id' => 4,
'id_parent' => 3,
'url' => 'blog/kitchen/salads',
'childs' => array()
),
array(
'id' => 5,
'id_parent' => 3,
'url' => 'blog/kitchen/soups',
'childs' => array()
),
)
),
)
),
array(
'id' => 3,
'id_parent' => 2,
'url' => '/blog/kitchen',
'childs' => array()
),
array(
'id' => 4,
'id_parent' => 3,
'url' => 'blog/kitchen/salads',
'childs' => array()
),
array(
'id' => 5,
'id_parent' => 3,
'url' => 'blog/kitchen/soups',
'childs' => array()
),
);
What I need:
array(
array(
'id' => 1,
'id_parent' => NULL,
'url' => '/',
'childs' => array ()
),
array(
'id' => 2,
'id_parent' => NULL,
'url' => '/blog',
'childs' => array (
array(
'id' => 3,
'id_parent' => 2,
'url' => 'blog/kitchen',
'childs' => array(
array(
'id' => 4,
'id_parent' => 3,
'url' => 'blog/kitchen/salads',
'childs' => array()
),
array(
'id' => 5,
'id_parent' => 3,
'url' => 'blog/kitchen/soups',
'childs' => array()
),
)
),
)
),
);
If there is an easier way, like; SELECT
directly in the database it would be great, if not, just removing the inner items from the outer layer with PHP is enough.