Laravel: How do I parse this json data in view blade?
Asked Answered
T

8

40

Currently this is my view

{{ $leads }}

And this is the output

{"error":false,"member":[{"id":"1","firstName":"first","lastName":"last","phoneNumber":"0987654321","email":"[email protected]","owner":{
"id":"10","firstName":"first","lastName":"last"}}]}

I wanted to display something like this

Member ID: 1
Firstname: First
Lastname: Last
Phone: 0987654321

Owner ID: 10
Firstname: First 
Lastname: Last
Tabret answered 6/5, 2015 at 9:44 Comment(0)
H
52

It's pretty easy. First of all send to the view decoded variable (see Laravel Views):

view('your-view')->with('leads', json_decode($leads, true));

Then just use common blade constructions (see Laravel Templating):

@foreach($leads['member'] as $member)
    Member ID: {{ $member['id'] }}
    Firstname: {{ $member['firstName'] }}
    Lastname: {{ $member['lastName'] }}
    Phone: {{ $member['phoneNumber'] }}

    Owner ID: {{ $member['owner']['id'] }}
    Firstname: {{ $member['owner']['firstName'] }} 
    Lastname: {{ $member['owner']['lastName'] }}
@endforeach
Hippocampus answered 6/5, 2015 at 9:59 Comment(0)
V
15

For such case, you can do like this

@foreach (json_decode($leads->member) as $member)
     {{ $genre }}
@endforeach
Vankirk answered 16/9, 2018 at 11:42 Comment(2)
This is awesome for me, because I want to use blade for some views and vue for others but don't want 2 different controller files (one returning json and the other for blade). I can just return json data to both and it works. That is awesome!Sector
This perfecto. Big up @BijayaTref
C
11

Example if you have array format like this:

$member = [
    [ "firs_name" => "Monica",
      "last_name" => "Dev",
      "sex" => "F"
    ],
    [ "firs_name" => "Blake",
      "last_name" => "Devante",
      "sex" => "M"
    ],
    [ "firs_name" => "Johnny",
      "last_name" => "Merritt",
      "sex" => "M"
    ]
]

You can use @json Blade directive for Laravel 5.5 to 9.x

<script>
 var app = @json($member);
</script>

From Laravel 8.x to latest version you can use Illuminate\Support\Js::from method directive.

<script>
var app = {{ Illuminate\Support\Js::from($member) }};
</script>

And for short with Js facade

<script>
var app = {{ Js::from($array) }};
</script>

Reference:

https://laravel.com/docs/blade

Chapbook answered 23/2, 2022 at 16:31 Comment(2)
The (low quality, 7 year old) question is about parsing JSON data, not turning a PHP variable into JSON.Forta
@miken32, Yes, It is old but it is still value until now. Yes, we are talking about parsing JSON data in Laravel view that all most array data or object in PHP and we still use Laravel until now and these Blade directive still value to using. So I wrote a bit more detail how to parsing JSON data in Laravel view for Javascript variables.Chapbook
B
6

You can use json decode then you get php array,and use that value as your own way

<?php 
$leads = json_decode($leads, true);
dd($leads);
Bernadinebernadotte answered 6/5, 2015 at 10:3 Comment(0)
G
6

The catch all for me is taking an object, encoding it, and then passing the string into a javascript script tag. To do this you have to do some replacements.

First replace every \ with a double slash \\ and then every quote" with a \".

$payload = json_encode($payload);
$payload = preg_replace("_\\\_", "\\\\\\", $payload);
$payload = preg_replace("/\"/", "\\\"", $payload);
return View::make('pages.javascript')
  ->with('payload', $payload)

Then in the blade template

@if(isset($payload))
<script>
  window.__payload = JSON.parse("{!!$payload!!}");
</script>
@endif

This basically allows you to take an object on the php side, and then have an object on the javascript side.

Glorification answered 5/10, 2017 at 15:33 Comment(1)
Killing flies with cannon shotsAlcott
M
3

in controller just convert json data to object using json_decode php function like this

$member = json_decode($json_string); 

and pass to view in view

return view('page',compact('$member'))

in view blade

Member ID: {{$member->member[0]->id}}
Firstname: {{$member->member[0]->firstname}}
Lastname: {{$member->member[0]->lastname}}
Phone: {{$member->member[0]->phone}}

Owner ID: {{$member->owner[0]->id}}
Firstname: {{$member->owner[0]->firstname}}
Lastname: {{$member->owner[0]->lastname}}
Mediaeval answered 6/5, 2015 at 9:59 Comment(0)
R
2

If your data is coming from a model you can do:

App\Http\Controller\SomeController

public function index(MyModel $model)
{
    return view('index', [
        'data' => $model->all()->toJson(),
    ]);
}

index.blade.php

@push('footer-scripts')
  <script>
    (function(global){
      var data = {!! $data !!};
      console.log(data);
      // [{..}]
    })(window);
  </script>
@endpush
Representational answered 19/4, 2018 at 20:23 Comment(0)
B
0

Just Remove $ in to compact method ,
return view('page',compact('member'))

Bevash answered 16/3, 2018 at 15:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.