Murphz
Murphz

Reputation: 33

Select value from JSON inside Laravel Blade

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

Upvotes: 2

Views: 3591

Answers (3)

LF-DevJourney
LF-DevJourney

Reputation: 28534

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

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

Upvotes: 0

Sapnesh Naik
Sapnesh Naik

Reputation: 11656

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`

Upvotes: 1

Dinoop
Dinoop

Reputation: 497

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

Upvotes: 0

Related Questions