Select value from JSON inside Laravel Blade

北城余情 提交于 2019-12-11 02:47:40

问题


I'm using laravel with blade and vuejs. Using a controller for a list I am receving the correct response with all the users data to show.

Some of this fields are json data and I can't understand how to retrieve single value.

`@foreach ($lista as $freelance)
  <md-table-row>
    <md-table-cell>
      {{ $freelance->regione }}
     </md-table-cell>
  </md-table-row>
@endforeach`

$freelance->regione is a JSON, correctly shown in the page as:

`{"text": "Veneto", "value": "Veneto"}`

My question is, how can I get the single value of the JSON response and not all the data? Preferably without loops...I know that I can use a new loop for it but possible no..


回答1:


Try this in your controller before passing the $lista variable to view do this.

foreach($lista as list)
{

 //we will decode the variable befoe passing it to view
 $list->regione = json_decode($list->regione, true);


}

and then pass the variable to your view like :

    return View::make('your_view_name', compact('lista'));

then in your blade view.

`@foreach ($lista as $freelance)
  <md-table-row>
    <md-table-cell>
      {{ $freelance->regione['text'] }}
     </md-table-cell>
    <md-table-cell>
      {{ $freelance->regione['value'] }}
     </md-table-cell>
  </md-table-row>
@endforeach`



回答2:


There are functions laravel blade template allow use directly. You need to decode the json string in order to get the value in it.

@foreach ($lista as $freelance)
 <md-table-row>
    <md-table-cell>
        {{ json_decode($freelance->regione)->value }}
    </md-table-cell>
 </md-table-row>
@endforeach



回答3:


You can use json_decode to parse the json string, refer to this for json parse

{{ json_decode($freelance->regione, true)['value'] }}


来源:https://stackoverflow.com/questions/43881258/select-value-from-json-inside-laravel-blade

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!