【发布时间】:2020-08-10 21:52:12
【问题描述】:
发生了一些奇怪的事情。
我有一个这样的数组:
=> [
"optionalinformation" => [
"domain" => [
"type" => "string",
],
],
]
这个数组被一个资源使用,如果我使用 tinker 来检查这个资源,像这样:
$result = App\Http\Resources\ProductResource::make(Product::find(2));
is_array($result->optionalinformation);
在这种情况下,结果是true:这是一个数组。
但是如果 axios 获取结果,我会得到这个:
"optionalinformation": {
"domain": {
"type": "string"
},
它不再是一个数组而是一个对象。任何想法为什么会发生这种情况?
这是我的 api 资源:
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'optionalinformation' => $this->optionalinformation,
];
}
【问题讨论】:
-
你期待什么样的结果?
-
好吧:我希望有一个数组。
-
请贴出js的例子。
标签: laravel vue.js axios laravel-7 laravel-resource