我正在使用Guzzle发出get请求,我有一个Json响应。我试图将Json响应传递给我的视图,但是得到了像title这样的错误。 这是我的控制器:
class RestApi extends Controller
{
public function request() {
$endpoint = "http://127.0.0.1:8080/activiti-rest/service/form/form-data?taskId=21159";
$client = new \GuzzleHttp\Client();
$taskId = 21159;
$response = $client->request('GET', $endpoint, ['auth' => ['kermit','kermit']]);
$statusCode = $response->getStatusCode();
$content = json_decode($response->getBody(), true);
$formData = $content['formProperties'];
return view('formData')->with('formData', $formData);
}
}
当我使用dd(formData)时,数据不为null:
在我看来,我只想检查我的formData是否通过:
@if(isset($formData))
@foreach($formData as $formDataValue)
{{ $formDataValue }}
@endforeach
@endif
这是我的路线:
Route::get('/response','RestApi@request')->middleware('cors');
我怎样才能解决这个问题?非常感谢你!
你试图直接显示数组{{ $formDataValue }}
{{ }}
这只支持字符串
它应该是这样的例如:
@if(isset($formData))
<ul>
@foreach($formData as $formDataValue)
<li>ID : {{ $formDataValue['id'] }}</li>
<li>Name : {{ $formDataValue['name'] }}</li>
<li>Type : {{ $formDataValue['type'] }}</li>
.
.
.
@endforeach
</ul>
@endif
所以,
@if(isset($formData)) //$formData is an array of arrays
@foreach($formData as $formDataValue)
{{ $formDataValue }} //$formDataValue is still an array, so it fails
@endforeach
@endif
{{ }}
只接受字符串。
你必须再次迭代:
@foreach($formData as $formDataItem)
@foreach($formDataItem as $item)
//here $item would be a string such as "id", "name", "type", but can also be an array ("enumValues")
@endforeach
@endforeach
最后
@foreach($formData as $formDataItem)
@foreach($formDataItem as $item)
@if(is_array($item))
@foreach($item as $i) //iterate once more for cases like "enumValues"
//here you'll have one of "enumValues" array, you have to iterate it again :)
@endforeach
@else
{{ $item }} //you can render a string like "id", "name", "type", ...
@endif
@endforeach
@endforeach