Search code examples
laravellaravel-5laravel-5.3whmcs

How to decode the json data from WHMCS and display the data using foreach loop in laravel-5.6


The controller code is given below:

class GetProductController extends Controller
{
 public function show(){

    $products = Whmcs::GetProducts([
        'pid'=>'pid',
        'name' =>'name',
        'price' =>'price',
        'description' =>'description'


    ]);

    return view('main.SME_Hosting',['products'=>$products]);


    }
}

The code using foreach is as follows:

@foreach ($products as $product)
           {{$product}}



@endforeach 

I'm getting an error as Expecting string not an array given.. and Undefined variable 'pid'.

The Route code is:

Route::get('SME_Hosting','GetProductController@show'); 

Suggest a solution to decode the json data and display it.


Solution

  • Try this: class GetProductController extends Controller { public function show(){

    $products = Whmcs::GetProducts([
        'pid',
        'name',
        'price',
        'description'
    ]);
    
    return view('main.SME_Hosting',['products'=>$products]);
    }
    

    And in view:

    @foreach ($products as $product)
           {{$product->name}}
    @endforeach