skip to Main Content

I have a json column called additional_info. I want to be able to return the data held there with foreach as follows.

<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>

… like this

{
  ....., 
  "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",
  },
  ....
}

how can I do that? There are dozens of data like this.

2

Answers


  1. Chosen as BEST ANSWER
    @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
    

    i have run this code but i am not getting the result i want

    enter image description here


  2. Add a cast on yout model

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

    This cast will already convert the json to vector at the time of feeding the model

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search