laravel8 上的未定义索引

Undefined index on laravel8

我正在学习显示 public API 来自 http://batikita.herokuapp.com/index.php/batik/all

的数据

我尝试使用以下语法显示数据:

InfoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

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>

结果是这样的: enter image description here

显示来自 public API 和 laravel8 的数据的正确语法是什么?请帮忙,谢谢。

您的错误实际上与 Laravel 无关,只是一些基本的 php.

您应该循环 $data['hasil'] 并显示 $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

当你使用 foreach 循环时,你可以像这样使用

 @foreach($data as $dataBatik)
    {{ $dataBatik['link_batik'] }}
 @endforeach