Home How can I prettify Join Result on Rest API response object?
Reply: 0

How can I prettify Join Result on Rest API response object?

user960
1#
user960 Published in April 26, 2018, 8:50 pm

I started learning about building REST API using PHP as core language and I came into issue with creating response object. I have table users and user_social inside my MySQL database. What I am trying to achieve is to get response object to be like this:

{
    id,
    name,
    age,
    social: [
        twitter,
        facebook,
        github
    ]
}

My sql query looks like this currently:

select users.id, users.name, users.age, user_social.* from users inner join user_social on users.id = user_social.user_id limit 10;

And my JSON response looks like this currently:

{
    id,
    name,
    age,
    twitter,
    facebook,
    github
}

So the question is, how can I group results from join to be in structure like I want to be? Is there MySQL way or I should map it by looping through each record inside PHP and then send response.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.398626 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO