You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Blade视图中如何访问并打印package_list数据

Fixing Nested JSON Access in Laravel View

Hey there! I see exactly what's going on here—your package_list is still a raw JSON string even after you decoded the outer packages data. That’s because your original $resource->packages is a nested JSON structure: the top layer is an array, but each item’s package_list field is itself another JSON string that didn’t get parsed in your initial decode. Let’s fix this properly.

Root Cause Breakdown

When you run json_decode($resource->packages, true), you’re only parsing the top-level array. The package_list value inside each entry stays a string, which is why trying to access $packageDetail['package_list']['name'] throws an error—you’re treating a string like an array.

Stick to MVC best practices by handling data processing in the Controller, keeping your View focused solely on display. We’ll decode the outer array first, then loop through each item to parse the nested package_list:

// Decode the top-level packages array
$rawPackages = json_decode($resource->packages, true);

// Process each package to decode its nested package_list
$processedPackages = array_map(function($package) {
    // Only decode if package_list exists and is a string
    if (isset($package['package_list']) && is_string($package['package_list'])) {
        $package['package_list'] = json_decode($package['package_list'], true);
    }
    return $package;
}, $rawPackages);

// Pass fully processed data to the view
$this->data['naskleng'] = $processedPackages;

Now in your View, you can directly access the nested fields without extra work:

@forelse($naskleng as $packageDetail)
    <div class="package-item">
        <p>ID: {{ $packageDetail['package_list']['id'] }}</p>
        <p>Name: {{ $packageDetail['package_list']['name'] }}</p>
    </div>
@empty
    <p>No package details available.</p>
@endforelse

Solution 2: Decode in the View (Emergency Workaround)

If you need a quick fix without touching the Controller, you can decode the nested JSON directly in the View. Note this isn’t ideal for long-term maintainability, but it works for testing:

@forelse($naskleng as $packageDetail)
    @php
        // Parse the package_list string into an array
        $packageList = json_decode($packageDetail['package_list'], true);
    @endphp
    <div class="package-item">
        <p>ID: {{ $packageList['id'] }}</p>
        <p>Name: {{ $packageList['name'] }}</p>
    </div>
@empty
    <p>No package details available.</p>
@endforelse

Quick Verification Tip

After processing in the Controller, add a dd($processedPackages) to double-check that package_list is now an array instead of a string:

$this->data['naskleng'] = $processedPackages;
dd($this->data['naskleng']); // Confirm the structure here before rendering the view

内容的提问来源于stack exchange,提问作者AgoRaCLX

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:59:12