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:
- 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]
}
- 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?
usersdata to only ids ?with, that should be 2 queries, so that is the best thing (as you shared).