My application involves three models on the frontend (using Vue/Vuex-ORM): Category
, CategoryItem
, and Item
.
I have successfully fetched an array of categories, each containing an array of items. The relationships between these models are defined in an intermediate join model, allowing me to access them like this:
// array of categories with nested items
const categories = Category.query().where('pack_id', this.selectedPack.id).with('items').get();
categories.map(category => {
category.items.forEach(item => {
console.log('item.pivot: ', item.pivot); // accessing join model
// how can I order items based on item.pivot?
})
})
As I iterate through each category's items with .forEach
, I want to sort them based on item.pivot.position
.
Initially, I attempted to create a new empty array inside the .map
function and then push values based on position comparison. However, I struggled to implement this logic effectively.
Any suggestions or guidance would be greatly appreciated!