Home > Mobile >  Get data from array (from an API) in Laravel
Get data from array (from an API) in Laravel

Time:05-05

It is my first time working with an external API. I have already fixed that it gives me the right output. In a controller I have the following code:

public function index() {
    $api = new Wefact();

    $parameters = [
    ];

    $api_response = $api->sendRequest('product', 'list', $parameters);

    print_r($api_response);

}

This shows me the array on the page. But I do not know how to use this array to get it into a foreach in the blade. The output of the print_r is as follow:

Array
(
    [controller] => product
    [action] => list
    [status] => success
    [date] => 2022-05-05T04:20:03 02:00
    [totalresults] => 2
    [currentresults] => 2
    [offset] => 0
    [products] => Array
        (
            [0] => Array
                (
                    [Identifier] => 1
                    [ProductCode] => P0001
                    [ProductName] => SIM ONLY 5GB
                    [ProductKeyPhrase] => SIM ONLY 5GB
                    [ProductDescription] => 
                    [NumberSuffix] => 
                    [PriceExcl] => 25
                    [TaxCode] => V21
                    [TaxPercentage] => 21
                    [PricePeriod] => m
                    [Modified] => 2022-05-05 03:49:57
                )
 
            [1] => Array
                (
                    [Identifier] => 2
                    [ProductCode] => P0002
                    [ProductName] => SIM ONLY 10GB
                    [ProductKeyPhrase] => SIM ONLY 10GB
                    [ProductDescription] => 
                    [NumberSuffix] => 
                    [PriceExcl] => 35
                    [TaxCode] => V21
                    [TaxPercentage] => 21
                    [PricePeriod] => m
                    [Modified] => 2022-05-05 04:03:47
                )
 
        )
 
)

As you can see there are two products. I want to have these in a datatable with an foreach. Since it is my first time, I really do not know how to do this.

Anyone that can help me out?

CodePudding user response:

If you want to access the array of products it would be like this

return view('your_view', [
  'products' => $api_response['products']
]);
  • Related