【问题标题】:Unable to display all data from mysql json column无法显示来自 mysql json 列的所有数据
【发布时间】:2020-02-08 18:18:28
【问题描述】:

我正在使用 first() 从多个连接表中检索单行数据,其中包括一个名为 properties 的 json 列。我可以在刀片模板中显示所有非 json 数据。

但是,对于名为 properties 的 json 列,我正在拔头发,花费无数小时谷歌搜索,看着她但无济于事。

如果有人能指出令人眼花缭乱的显而易见的事情(我敢肯定)

我也尝试过并达到了可以访问对象根目录中的任何内容但没有嵌套内容的地步。

如此漂亮的 dd($quote->properties) 返回;

"{
  "mode": 1,
  "service": 1,
  "rates": {
    "DAP": 825.22
  },
  "detail": {
    "weights": {
      "actual": 111.00,
      "volume": 0,
      "chargeable": 111.00
    }
  }
}"

感谢前导和尾随 "s 仅添加用于在浏览器中呈现,实际列中不存在。

使用 {{ json_decode($quote->properties,true)['mode'] }}

结果 = 1,这是所需的输出。

然而{{ json_decode($quote->properties,true)['rates']['DAP'] }}

结果 = 未定义索引:DAP

想要的 = 825.22

{{ var_dump(json_decode($quote->properties,true)) }}

    array (size=4)
      'mode' => int 1
      'service' => int 1
      'rates' => 
        array (size=1)
          'DAP' => float 825.22
      'detail' => 
        array (size=1)
          'weights' => 
            array (size=3)
              'actual' => string '111.00' (length=6)
              'volume' => int 0
              'chargeable' => string '111.00' (length=6)

【问题讨论】:

  • 我只在你的json中看到'DAP',我没有看到'DDU'
  • 谢谢,我已更新票证以更正,因为这是一个错字,但结果是一样的。
  • 可能该“类型”的某些实例没有设置 DAP 属性...这就是您在特定情况下看到错误的原因。顺便说一句.. 为所有发生的情况转储 json_decode 并亲眼看看 [rates][dap] 寻址为什么以及何时没有成功
  • 你可以试试{{ json_decode($quote->properties,true)['rates']['DAP'] ?? 'DAP not set' }}
  • @DiegoDeVita 已编辑发布以包含 var_dump

标签: laravel-5 laravel-blade


【解决方案1】:

谢谢大家,我不知道为什么要工作......

也许我有一个错字并重新添加了固定或清除的视图缓存??

感谢大家的回复。

【讨论】:

    猜你喜欢
    • 2021-11-23
    • 2015-12-31
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    相关资源
    最近更新 更多