在Laravel中使用Axios检索数组

时间:2017-10-20 13:10:24

标签: php laravel vue.js axios

我正在向我的某个控制器功能发出Axios请求,这些功能进入我的数据库并获取所有供应商的信息但是当它返回我的VUE文件时没有数据

这是我的要求:

mounted(){
            axios.get('/product/'+ this.url + '/suppliers/info').then(response=>console.log(response.data));

        },

这是我的控制器功能

public function getSuppliers($product){

        $suppliers = $this->supplier->getAll();

        return response()->json($suppliers);
    }

当我在控制器中输出$供应商时,它拥有所有数据,所以我知道它已从数据库中检索到成功,但是当我在控制台返回VUE后将其注销后,数据就消失了。

这就是我所看到的,如果我只是控制日志响应: - enter image description here

我可以看到它返回应该存在的所有数组,但它们都没有任何数据?我做错了什么?

当我在控制器中进行DD时,这是$供应商的片段: enter image description here

来自网络的请求标头图片: - enter image description here

getAll功能: -

public function getAll()
{
    return $this->buildEntities($this->data->orderBy('name')->get());

}

1 个答案:

答案 0 :(得分:1)

你的$suppliers似乎不是一个数组,而是一个雄辩的模型。所以试试这个:

return response()->json(["suppliers"=>$suppliers], 200);

请参阅response.data.suppliers

**编辑**

问题可能是函数buildEntity。 试着

$this->data->orderBy('name')->get()