Skip to content
Advertisement

laravel json data listing

I’m getting json data but I can’t list it in datatable

Controller File

 public function index()
{
      $data = Http::get('https://jsonplaceholder.typicode.com/posts');
    return view('frontend.default.index', ['data'=> $data->json()]);
}

View File

      <table id="table_id" class="display">
        <thead>
            <tr>
                <th>Column 1</th>
                <th>Column 2</th>
            </tr>
        </thead>
        <tbody>
            @foreach ($data as $key => $value)
            <tr>
                <td>
                {{$value->title}} // not working
               </td>
                <td>Row 1 Data 2</td>
            </tr>
            @endforeach
        </tbody>
    </table>

enter image description here

Advertisement

Answer

The data you pull from json comes as an array. You are trying to print as an object while you print.

Solution

 @foreach ($data as $key => $value)
        <tr>
            <td>
            {{$value['title']}} // not working
           </td>
            <td>Row 1 Data 2</td>
        </tr>
        @endforeach
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement