Laravel vue无法访问关系数据

时间:2017-01-20 07:42:13

标签: laravel vue.js

我的客户数据与地址表有一个关系。 返回的数据格式如下所示。

{
  "customer_id": 1,
  "last_name": "Cruickshank",
  "first_name": "Pearl",
  "phone": "+4 921-008-8344",
  "email": "kristofer.kautzer@example.org",
  "order_id": null,
  "address_id": 66,
  "created_at": "2017-01-18 06:24:40",
  "updated_at": "2017-01-18 06:24:40",
  "deleted_at": null,
  "address": {
    "address_id": 1,
    "address_code": "RS-03549-9811",
    "address1": "14978 Effertz Turnpike Apt. 086",
    "address2": null,
    "city": "Susanaburgh",
    "province": "Illinois",
    "country": "PG",
    "postal_code": "03549-9811",
    "created_at": "2017-01-18 06:24:40",
    "updated_at": "2017-01-18 06:24:40"
  }

我使用return $this->model->with(implode(',', $relation))->get();

获取数据

但是,我无法访问地址对象中的任何数据。

Vue Code 我扔城市是未定义的。但是,我可以使用chrome开发人员工具

访问地址对象数据
<tr v-for="(customer, index) in customers">
     <td>{{index + 1}}</td>
     <td>{{getFullName(customer.first_name, customer.last_name)}}</td>
     <td>{{customer.phone}}</td>
     <td>{{customer.address.city}}</td>
</tr>

我做错了吗?我也试过了customer['address']['city']

我可以在获取数据时访问城市,如下所示。

getCustomerList () {
    axios.get('/api/customers')
       .then((res)=>{
          if(res.status === 200){
            //I can access city here but not in vue for loop ...
            console.log(res.data.customers[0].address.city)
            this.customers = res.data.customers
        }
    })
 },

0 个答案:

没有答案