0

I am trying to show the data that comes with the JSON format on the page. Not all names in the JSON file are the same. For example, some of them have data named sale_commission, but some do not.

You can look at the sample JSON file below

{
        "name":"example_name2",
        "fname":"example_fname2",
        "lname":"example_lname1",
        "affiliate_id":111111,
        "email":"example1@gmail.com",
        "page_views":"32",
        "unique_visits":10,
        "sale_commission":"4.99",
        "num_orders":1,
        "order_amount":"0.00"
    },
    {
        "name":"example_name2",
        "fname":"example_fname2",
        "lname":"example_lname2",
        "affiliate_id":22222,
        "email":"example2@gmail.com",
        "page_views":"5",
        "unique_visits":1
    },

I can get the ones with the same data name in the whole json file, but the ones that don't exist in all give errors

My Laravel blade code is below

@foreach($datas['data'] as $i => $v)
<tr>
  <th>{{ $i }}</th>
  <td>{{ $v['fname'] }}</td>
  <td>{{ $v['sale_commission'] }}</td>
  <td>{{ $v['unique_visits'] }}</td>
</tr>
@endforeach

I get an error when I use the variable {{ $v['sale_commission'] }}

How can fix this problem?

Thanks

Alon Eitan
  • 11,997
  • 8
  • 49
  • 58
hakkidilek
  • 153
  • 3
  • 20

1 Answers1

0

You can try with Arr::get() or data_get() helpers. Example:

Arr::get($v, 'sale_commission') or data_get($v, 'sale_commission');
Thai Nguyen Hung
  • 1,154
  • 5
  • 13