Skip to content
Advertisement

Undefined index on laravel8

I’m learning to display public API data from http://batikita.herokuapp.com/index.php/batik/all

I have tried to display data with the following syntax:

InfoController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use IlluminateSupportFacadesHttp;

class InfoController extends Controller
{
    public function index() {
        $response = Http::get('http://batikita.herokuapp.com/index.php/batik/all');
        $data = $response->json();
        return view('index', compact('data'));
    }
}

index.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
</head>
<body>
    <div class="container">
        <table class="table">
            <thead class="thead-dark">
                <tr>
                <th scope="col">#</th>
                <th scope="col">Appearance</th>
                <th scope="col">Batik Name</th>
                <th scope="col">Origin</th>
                <th scope="col">History</th>
                <th scope="col">Lowest Price</th>
                <th scope="col">Highest Price</th>
                </tr>
            </thead>
            <tbody>
                @php
                    $no = 0;
                @endphp
                @foreach ($data as $dataBatik)
                    @php
                        $no++;
                    @endphp
                    <tr>
                        <th scope="row">{{ $no }}</th>
                        <td>{{ $dataBatik['hasil']['link_batik'] }}</td>
                        <td>{{ $dataBatik['hasil']['nama_batik'] }}</td>
                        <td>{{ $dataBatik['hasil']['daerah_batik'] }}</td>
                        <td>{{ $dataBatik['hasil']['makna_batik'] }}</td>
                        <td>{{ $dataBatik['hasil']['harga_rendah'] }}</td>
                        <td>{{ $dataBatik['hasil']['harga_tinggi'] }}</td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</body>
</html>

And the result is like this: enter image description here

What is the correct syntax for displaying data from the public API with laravel8? Please help, thank you.

Advertisement

Answer

Your error doesn’t actually have something to do with Laravel, just some basic php.

You should loop on $data['hasil'] and display information like $dataBatik['link_batik'].

@foreach ($data['hasil'] as $dataBatik)
    ...
    <tr>
        <th scope="row">{{ $no }}</th>
        <td>{{ $dataBatik['link_batik'] }}</td>
        <td>{{ $dataBatik['nama_batik'] }}</td>
        <td>{{ $dataBatik['daerah_batik'] }}</td>
        <td>{{ $dataBatik['makna_batik'] }}</td>
        <td>{{ $dataBatik['harga_rendah'] }}</td>
        <td>{{ $dataBatik['harga_tinggi'] }}</td>
    </tr>
@endforeach
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement