0

I build simple SPA. I use laravel on backend, Vuex with vuex and router on frontend. Let's assume i have endpoint:

/users/1/conversations/1

I have defined relation between users and conversations. Controller method is something like this:

User::findOrFail(id)->conversations()->with('user')->get();

And it gives example output:

[{
id: 1,
title: 'lorem ipsum'
users: [{id: 1, name:'John'}, {id: 2, name: 'Bob'}]
},
id: 2,
title: 'lorem ipsum'
users: [{id: 1, name:'John'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Ann'}]
}]

And data for users that participates in many conversations is repeated. In example above it's: {id: 1, name:'John'} I think data repetition is not a good idea. Maybe i should do something like this:

  1. Modify controller method to produce this output :
{ 
conversations: [{
id: 1,
title: 'lorem ipsum'
users: [1,2]
},
id: 2,
title: 'lorem ipsum'
users: [1,2,3]
}],
unique_user_ids: [1,2,3]
}
  1. Then in my frontend call two different axios.get: once for conversations (/users/{id}/sessions), n-times for each user (/user/{id}).

Does it have any sense?

4
  • So, you want to shorten users data to only ids ? Commented Mar 22, 2021 at 17:42
  • Yes. And I know how to shorten this. The question is: should i do this? I don't know whats better calling axios n-times after i send just ids for getting them without duplicates or just get repeated data and dont axios /user/{id} n times Commented Mar 22, 2021 at 17:47
  • or should I edit my backend code to do it via once and produce this response: { conversations: [{ id: 1, title: 'lorem ipsum' users: [1,2] }, id: 2, title: 'lorem ipsum' users: [1,2,3] }], unique_users:[ { id: 1, name: ..., }, { id: 2, name: ... }, { id: 3, name: ... }] } Commented Mar 22, 2021 at 17:49
  • I understand now... The answer is: it depends. Doing 40 axios calls is not a big deal (you should not do 40 at once, but if you have to there is no problem), what you should avoid doing is hitting the DB 40 times when you could do it 2 times and maybe process that data in what you want... So you have to have that in mind, if you are doing a query using with, that should be 2 queries, so that is the best thing (as you shared). Commented Mar 22, 2021 at 17:54

1 Answer 1

1

Data repetition is not an issue normally unless you have big data with too many repetitions. The problem would be the number of queries and total usage of memory if your concern is performance. To check them you can use Laravel Debugbar.

Actually, if you fetch users separately, you will execute too many extra queries, which will decrease the performance.

If you plan fetching user data when the conversation is displayed, it will increase the page load performance due to the smaller data size, but in this case, your server will have to respond to too many extra requests, which can create other performance issues.

So, as always, it depends...

Personally, I always start with the data that is absolutely necessary. All other things can be delivered by request or time. So, the 2nd option looks better.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for response. I'll filter this route to output something like this: { conversations: [{ id: 1, title: 'lorem ipsum' users: [1,2] }, id: 2, title: 'lorem ipsum' users: [1,2,3] }], unique_users:[ { id: 1, name: ..., }, { id: 2, name: ... }, { id: 3, name: ... }] }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.