提问者:小点点

如何在laravel刀片视图中显示JSON数据


我将得到一个JSON响应

@foreach($logs as $log)
  <div class="modal-body">
    {{ $log->general }}
  </div>

  <div class="modal-body">
    {{ $log->response_headers }}
  </div>
@endforeach

响应的结构不是这样的,我现在已经使其具有可读性

General
{
  "host": "abcd-io.test",
  "path": "api/v1/companies/hello.com",
  "request_ip": "127.0.0.1"
}

response_headers
{
  "X-Powered-By": [
    "Express"
  ],
  "Access-Control-Allow-Origin": [
    "*"
  ],
  "Content-Type": [
    "application/json; charset=utf-8"
  ],
  "Content-Length": [
    "4857"
  ],
  "ETag": [
    "W/\"12f9-UhKH0rSAm7BiHIeW5pbrH1gphXs\""
  ],
  "Date": [
    "Sat, 20 Jun 2020 12:51:28 GMT"
  ],
  "Connection": [
    "keep-alive"
  ]
}

控制器

public function index() {
   $logs = Log::where('user_id',auth()->user()->id)
          ->orderBy('created_at', 'DESC')->get();

   return view('api.logs', compact('logs'));
}

我希望分别显示hostabcd-io.test

我尝试使用{{$log->general['host']}},但没有工作


共1个答案

匿名用户

假设“general”是一个字符串,您可能必须使用json_decode($JSON)将JSON字符串转换为数组,并执行如下操作:

//Convert JSON to array
$json = json_decode($log->general, true);

检索主机

$json['host']