Home > Software engineering >  how can i display this data from an api which is in format of json and array
how can i display this data from an api which is in format of json and array

Time:02-08

What i want to achieve is to display this data in a dashboard for example risk name how many they are etc i using laravel how can i fetch it and display it to the blade

https://portal.zebratop.co.ke/api/v1/powerbi/dashboards/risk

this what i a have tried

    public function index()
    {
        $Total_users = User::all();

        $response = Http::acceptJson()
            ->get('https://portal.zebratop.co.ke/api/v1/powerbi/dashboards/risk');

        if($response->failed()){
            // Handle failure here
        }

        // Data was fetched successfully
        $data = collect($response->json());

        $data = $data->mapToDictionary(function($data, $key){
            $key = $data[0]['Business Unit'];
            //$key = $data[1]['Risk Name'];
            return [$key => $data];
        });
        
        $count = 0;

        $data = $data->map(function($data) use ($count){
            $count  = count($data[0]);
            return $data[0];
        });
        //dd($data);

        // Pass the data to view
        return view('home',compact('Total_users','data'));
        //return view('view.name', compact('data'));
    }

this the blade


<tr>
    <th  colspan="2"><h5 align ="center"><b>Business Unit</b>:{{$data->count()}}</h5></th>
</tr>
<tr>
     <th><h6><b>{{$data[Risk Name]}}</b></h6></th>
</tr>

CodePudding user response:

Try using curl to fetch the data from the endpoint.

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://portal.zebratop.co.ke/api/v1/powerbi/dashboards/risk',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'Cookie: XSRF-TOKEN=eyJpdiI6Im5sb0JHVTFjZUhkQ2xrRTl1Nlp5RFE9PSIsInZhbHVlIjoiSC9oYXZUMHZMSlpHa0sxbnBrUW12a2o2K014S25Ba2dmSzY3cDIwWWF5M2lwWXdDRnAvbHVmYXZMbnM4SitNeEtrdXQ2V2pucUxzdWFwa2dJWGZ5c2hHZ0I0L0NrV1hTZjNTRWZvOVRidzZ2SE5wbVFRcVhsenF4b3A4NFA3VUIiLCJtYWMiOiI2Y2EyM2ZjZDQzNWFjNjYzYjU1NTg0ZjI3Y2UwYjdhYzM1NmUwYmNhZGExZjViMmRlMTBiMTAyM2ViMDFmMzZhIn0=; risk_management_system_session=eyJpdiI6IkpKK215cktjWlc0Q1VWbnVxNmN4UUE9PSIsInZhbHVlIjoiZU9YbXhRUTdIQ2t5OEV5dWw2ZVJXT2lHMnBGSXhBMk9WSFhuS3pTbVhRTWRPZTFrUHRLMGo0Z1Y0aGMrZ25tZ2JkeXRrRi92WTUzMFRYTXhxVG9pWDQwN3JzQiszcnVkUTFzVGk3RDNJSHB3UWpjV1VPTEF6Zk54VDhxRGdDSEsiLCJtYWMiOiJlZmQ4MGM0NmI2M2YwN2FmOTkyODcxYTliY2RjZjI4ZWZlY2ZjYWIzZjE0ODBjY2M1MzM5MzNmNjU0MjRjNjJiIn0='
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

Then use json_decode($response) to convert into php array.

CodePudding user response:

Please change

return view('home',compact('Total_users','data'));

To

return view('home', ['data' => $data]);

Now you can access $data in blade file. eg. $data["Risk Name"]. if data is array of object then $data[0]["Risk Name"]

  •  Tags:  
  • Related