I have two tables articles and related_articles
table: articles
\+----+------------+
| id | title |
\+----+------------+
| 1 | Products |
| 2 | Info |
| 3 | Product#1 |
| 4 | Product#2 |
| 5 | Document |
\+----+------------+
table: related_articles
\+-----------+---------------+
| article_id| related_id |
\+-----------+---------------+
| 1 | 3 |
| 1 | 4 |
| 3 | 5 |
| 4 | 5 |
| 2 | 5 |
\+-----------+---------------+
article_id is foreign key for articles.id
related_id is foreign key for articles.id
Every article can have many parents and many children
I need nested json of all articles with related_articles, something like that:
[{
"id": 1,
"title": "Products",
"related": [
{ "id" : 3,
"title": "Product#1",
"related":[
{
"id" : 5,
"title": "Document",
"related":[]
}
]
},
{
"id" : 4,
"title": "Product#2",
"related":[
{
"id" : 5,
"title": "Document",
"related":[]
}
]
}
]
},
{
"id": 1,
"title": "Info",
"related": [
{
"id" : 5,
"title": "Document",
"related":[]
}
]
}]
I tried 'with recursive' but with no luck