Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

LearningLaravelYai's avatar

Best Practice: Normalize/transform data to frontend?

Hi.

I'm looking for the best way to transform db data, with relations from the db to the frontend..

Lets say we have two tabels:

Database Profile Table:
{
    "id": 3,
    "user_id": 10,
    "username": "Roberta",
    "online_at: "2019-08-02 10:32:33",
}


Database News Table:
{
    "id": 84,
    "profile_id": 3,
    "content": "...",
    "title": "...",
},

I can think of 3 ways this can be transformed:

A: With nesting

profiles: {
    "id": 10,
    "user_id": 10,
    "username": "Roberta",
    "online_at: "2019-08-02 10:32:33",
    "news" : [
        {   "id": 84,
            "profile_id": 3,
            "content": "...",
            "title": "...",
        },
    ]
}

B: More flat but still separate

data: [
    {
        profile:{
            "id": 10,
            "user_id": 10,
            "username": "Roberta",
            "online_at: "2019-08-02 10:32:33",
        },
        "news" : 
            {   "id": 84,
                "profile_id": 10,
                "content": "...",
                "title": "...",
            },
    }
]

C: Merge them all together (with risk of merge conflict in big db's)

data: [
    {
    "id": 3,
    "user_id": 10,
    "username": "Roberta",
    "online_at: "2019-08-02 10:32:33",
    "profile_id": 3,
    "content": "...",
    "title": "...",
    }
]

My thoughts are that B and C is the best when you want to insert the data into view components. Since the structure is the same every-time.

Where the structure of A can vary, depending on what controller you ask and what relations this controller has. In case A, the component is in most cases not designed to handle different nested objects. Also, you have to manually sit and correct the nested path in the component every-time. Not so copy paste friendly :D

Now, my question is, what do you usually do? Are there better ways to transform the data?

Thanks ;-)

0 likes
5 replies
bugsysha's avatar

Do not think how to do it, just use eloquent resources as suggested above.

cookie_good's avatar

+1 for a great question.

I don't do a lot of noSQL, but a SQL equivalent of lumping it all together into 1 table sometimes actually improves performance, if you have a unique primary key that the rest of data depends on. I would agree with the previous post. Try C. Run it through PHP Unit against the other 2 options.

As for @bugsysha and Eloquent Resources, again, I don't know a lot about noSQL in this scenario, but Eloquent works best when you are finding a needle in a haystack. If you are doing batches, you might find better alternatives out there.

bugsysha's avatar

Can you point out those better alternatives? But still I think I would go for a built in solution.

Snapey's avatar

Any time the data turns out to be 1:many then you need nesting or completely separate data sets.

If the top level is also a collection, you just end up trying to map two datasets back to each other in the frontend

I would always go for a nested option, and use eloquent and its relations to make this a breeze

Please or to participate in this conversation.