I got 2 array, one for states and another one for countries. I want to display the country name of the state. So, I'm using Laravel 5.7 for the backend and I get data using this for the countries Country::get(); and Vue.js recieve the array like this:
countries:Array(4)
0:created_at:(...)
deleted_at:(...)
id:(...)
name:(...)
shortName:(...)
updated_at:(...)
1:{…}
2:{…}
3:{…}
I do the same for the state as the country. State::get(); and vue recieve this:
states:Array(1)
0:country_id:(...)
created_at:(...)
deleted_at:(...)
id:(...)
name:(...)
shortName:(...)
updated_at:(...)
When I display the data into the table using v-for:
<tbody>
<tr v-for="state in data.states">
<td>{{SOMTHING TO DISPLAY COUNTRY NAME}}</td>
<td>{{state.name}}</td>
<td></td>
</tr>
</tbody>
At the first cell, I tried to do {{data.countries[state.country_id]['name']}} but it failed. Instead it display another country in the array where the key correspond to the country_id I gave.
I know that I can solve my problem by doing this State::with('country')->get(); and then into my vue component doing this state.country.name, but since I already send every country, I'm looking for another way to perform it.
Any idea?