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

Laravel 5.6中解码WHMCS JSON数据并使用foreach展示的问题

Fixing WHMCS Product Retrieval & Foreach Loop in Laravel 5.6

Let's break down and fix the issues you're facing step by step:

1. Fix Invalid WHMCS API Parameters

Your first problem is passing incorrect parameters to Whmcs::GetProducts(). The WHMCS GetProducts API endpoint doesn't accept fields like name, price, or description as request parameters—those are part of the response, not the request. The parameters you can pass are filters like pid (specific product ID), gid (product group ID), etc.

Corrected Controller Code (Initial Version)

class GetProductController extends Controller {
    public function show(){
        // Fetch all products (empty array = no filters)
        $response = Whmcs::GetProducts([]);
        
        // Optional: Check if API request succeeded
        if ($response['result'] !== 'success') {
            abort(500, 'Failed to load products: ' . $response['message']);
        }
        
        // Extract the actual products array from the response
        $products = $response['products'];
        
        return view('main.SME_Hosting', ['products' => $products]);
    }
}

Note: If your WHMCS wrapper returns objects instead of arrays (common in some packages), adjust the code to use object syntax:

if ($response->result !== 'success') { ... }
$products = $response->products;

2. Debug the Response Structure

To confirm the exact structure of the WHMCS response, add a dd() call in your controller to inspect the data:

$response = Whmcs::GetProducts([]);
dd($response); // This will show you the full response structure

This will help you verify if the products are under $response['products'] (array) or $response->products (object).

3. Fix the Foreach Loop in the View

Once you've correctly passed the $products array to the view, adjust your loop to access the product data properly. Depending on whether the products are arrays or objects:

If Products Are Arrays:

@foreach ($products as $product)
    <div class="product-item">
        <h3>{{ $product['name'] }}</h3>
        <p>Product ID: {{ $product['pid'] }}</p>
        <p>Price: {{ $product['price'] }}</p>
        <p>{{ $product['description'] }}</p>
    </div>
@endforeach

If Products Are Objects:

@foreach ($products as $product)
    <div class="product-item">
        <h3>{{ $product->name }}</h3>
        <p>Product ID: {{ $product->pid }}</p>
        <p>Price: {{ $product->price }}</p>
        <p>{{ $product->description }}</p>
    </div>
@endforeach

Why You Saw Those Errors

  • "Expecting string not an array given": This came from passing invalid parameters (like name => 'name') to the WHMCS API— the API was expecting a string value for a valid parameter, but received an array or incorrect input.
  • "Undefined variable 'pid'": This likely occurred because you were looping over the full API response (which includes keys like result and products) instead of the actual products array, so $product was a string (e.g., 'result') and trying to access $product->pid didn't make sense.

Additional Tips

  • Always check the WHMCS API documentation for valid parameters for the GetProducts endpoint.
  • If you need to filter products (e.g., by group), add valid parameters like ['gid' => 1] where 1 is your product group ID.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:33:27