Laravel json列显示在视图中

ie3xauqp  于 2023-02-25  发布在  其他
关注(0)|答案(2)|浏览(165)

我有一个名为additional_info的json列,我希望能够用foreach返回那里保存的数据,如下所示。

<a href="additional_info->Addslack->slack"><i class="additional_info->Addslack->icon"></i> additional_info->Addslack->title </a>
    
    
<a href="additional_info->Addgithub->github"><i class="additional_info->Addgithub->icon"></i> additional_info->Addgithub>title </a>

......像这样

{
  ....., 
  "Addphone":{
    "icon":"fas fa-phone-alt",
    "phone":"12345",
    "title":"work"
  },
  "Addskype":{
    "icon":"fab fa-skype",
    "skype":null,
    "title":null
  },
  "Addslack":{
    "icon":"fab fa-slack-hash",
    "slack":"slack_address",
    "title":null
  },
  "Addgithub":{
    "icon":"fab fa-github",
    "title":"git",
    "github":"cnahmetcn",
  },
  ....
}

我该怎么做呢?有很多这样的数据。

tp5buhyn

tp5buhyn1#

@foreach($card->additional_info as $key=>$value)
                            @if(is_object($value) || is_array($value))
                                @foreach($value as $key2=>$value2)
                                    @if ($value2 != null)
                                    {{$key2}} :  {{$value2}} <br>
                                    @endif
                                @endforeach
                            @endif
                            @endforeach

我已经运行这代码但是我没有得到我想要的结果

9nvpjoqh

9nvpjoqh2#

在yout模型上添加强制转换

protected $casts = [
  'column' => 'json'  
];

此强制转换在为模型提供数据时已经将json转换为vector

相关问题