1

So, this is my json format data

{
   "New Purchase Orders Created":{
      "date":"2018-02-26 14:05:14.000000",
      "timezone_type":3,
      "timezone":"UTC"
   },
   "colour":"bg-primary"
}

This is my view in blade.php

<ul class="list-group list-group-flush">
         @foreach( $notifications as $notification )
                <?php $notificationData = json_decode($notification->data, true);?>
         @foreach( $notificationData as $key=>$item )
                <li class="list-group-item text-white {{ $notificationData['colour'] }}">
                  @if ($key!='colour')
                      {{ $key }}<br>  
                      {{ $item['date'] }}

                  @endif
                </li>
          @endforeach
          @endforeach
</ul>

I want to get the data into the blade. So what should I do?

enter image description here

This is what i want to get.

Please help. Thanks.

2
  • Why is the notification title the key for the date? Commented Feb 26, 2018 at 14:47
  • @Phiter it's because this is notification view Commented Feb 26, 2018 at 14:59

2 Answers 2

1

Assuming the above JSON is an object array like this:

<?php
$notifications = '[{
   "New Purchase Orders Created":{
      "date":"2018-02-26 14:05:14.000000",
      "timezone_type":3,
      "timezone":"UTC"
   },
   "colour":"bg-primary"
},
{
   "New Purchase Orders Created":{
      "date":"2018-02-26 14:05:14.000000",
      "timezone_type":3,
      "timezone":"UTC"
   },
   "colour":"bg-primary"
}]';
?>

Thus, the script will be like this:

<ul class="list-group list-group-flush">
    @foreach( json_decode($notifications, true) as $notification => $data )
        <?php $item = $data[key($data)]; ?>
        <li class="list-group-item text-white {{ $data['colour'] }}">
            {{ key($data) }}<br>  
            {{ $item['date'] }}
        </li>
    @endforeach
</ul>
Sign up to request clarification or add additional context in comments.

2 Comments

Undefined offset: 0. Error =(
I made changes to my answer, can you please verify?
0

Use json_decode. This works with JSON you've shown:

{{ json_decode($json, true)['New Purchase Orders Created']['date'] }}

If New Purchase Orders Created key may be different in every JSON, use the array_first() helper:

{{ array_first(json_decode($json, true))['date'] }}

If you will have many elements, iterate over them:

$array = json_decode($json, true);
@foreach ($array as $key => $element)
    {{ $array[$key]['date'] }}
@endforeach

10 Comments

If not only just have the ['New Purchase Orders Created'], but ['Purchase Orders Arrived'] or somthing different can i still read? Or I have to change to using index? But how to use indexing?
@JweiWei if you'll use array_first() as I've shown, it doesn't matter what key the array has.
If now i got many and many data in, how should i get the second, third and fourth one?
@JweiWei iterate over the array with foreach(). I've updated the answer.
Its output me an error "Illegal string offset 'date'"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.