php - displaying api data in laravel blade view [laravel 8] not showing data

I'm fetching data from the imdb api and when i try to display the data in a blade view I'm facing a lot of errors.

probably because I'm not sure what is the response I'm getting from the api.

please bare with me and thanks in advance for taking the time to read through my question.

so here are some of the available example responses from the api:

{
  "d": [
    {
      "i": {
        "height": 1500,
        "imageUrl": "https://m.media-amazon.com/images/M/MV5BYTRiNDQwYzAtMzVlZS00NTI5LWJjYjUtMzkwNTUzMWMxZTllXkEyXkFqcGdeQXVyNDIzMzcwNjc@._V1_.jpg",
        "width": 1102
      },
      "id": "tt0944947",
      "l": "Game of Thrones",
      "q": "TV series",
      "rank": 36,
      "s": "Emilia Clarke, Peter Dinklage",
      "v": [
        {
          "i": {
            "height": 720,
            "imageUrl": "https://m.media-amazon.com/images/M/MV5BZTg4YzdjNTctNDg5Mi00ZmU1LTkzOWEtNmMyNDBjZjNhNTJiXkEyXkFqcGdeQXRyYW5zY29kZS13b3JrZmxvdw@@._V1_.jpg",
            "width": 1280
          },
          "id": "vi59490329",
          "l": "Official Series Trailer",
          "s": "3:19"
        },
        {
          "i": {
            "height": 1080,
            "imageUrl": "https://m.media-amazon.com/images/M/MV5BMTljMTZmNDUtNTEzNy00NDgyLTk2N2QtOTI3MGQyNWE0MTI5XkEyXkFqcGdeQWplZmZscA@@._V1_.jpg",
            "width": 1920
          },
          "id": "vi1097842713",
          "l": "The 8 Most Surprising Moments From \"Game of Thrones\" to Rewatch",
          "s": "3:39"
        },
        {
          "i": {
            "height": 720,
            "imageUrl": "https://m.media-amazon.com/images/M/MV5BMTg0ODM4NTc3OV5BMl5BanBnXkFtZTgwODAwODE1OTE@._V1_.jpg",
            "width": 1280
          },
  ],
  "q": "game of thr",
  "v": 1
}

I have tried to display the data in my blade view using a couple of ways the latest way I'm trying is this, blade.php:

@foreach ($data as $item)
    {{$item['d']}}
@endforeach

I'm getting this as a response:

{"data":{"d":[{"i":{"height":4096,"imageUrl":"https://m.media-amazon.com/images/M/MV5BMTg4NDA1OTA5NF5BMl5BanBnXkFtZTgwMDQ2MDM5ODE@.V1.jpg","width":2764},"id":"tt2582782","l":"Hell or High Water","q":"feature","qid":"movie","rank":1332,"s":"Chris Pine, Ben Foster","y":2016},{"i":{"height":755,"imageUrl":"https://m.media-amazon.com/images/M/MV5BMjM5ODQ5Nzc3OF5BMl5BanBnXkFtZTgwOTQzMzM4NjE@.V1.jpg","width":509}

the function in my Controller is this :

    public function api(Request $request)
    {
        $userInput = $request->input();

        $response = Http::withHeaders(
            [
                "x-rapidapi-host"=> "xxxxxxxxx",
                "x-rapidapi-key"=> "xxxxxxxxxxx",
            ]
        )->get("https://imdb8.p.rapidapi.com/auto-complete?q=",$userInput)->json();

        return json_encode(array('data'=>$response));


    }

I have also tired:

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

but got different errors like:

Illegal offset type.

and tried other ways as well but also didn't succeed.

what I'm I missing?? please help.

Answer

Solution:

Try this.
public function api(Request $request) { $userInput = $request->input();

    $response = Http::withHeaders(
        [
            "x-rapidapi-host"=> "xxxxxxxxx",
            "x-rapidapi-key"=> "xxxxxxxxxxx",
        ]
    )->get("https://imdb8.p.rapidapi.com/auto-complete?q=",$userInput);

    return response->json(array('data'=>$response->body()));
}

Answer

Solution:

If this is the response from api ,,,,

{"data":{"d":[{"i":{"height":4096,"imageUrl":"https://m.media-amazon.com/images/M/MV5BMTg4NDA1OTA5NF5BMl5BanBnXkFtZTgwMDQ2MDM5ODE@.V1.jpg","width":2764},"id":"tt2582782","l":"Hell or High Water","q":"feature","qid":"movie","rank":1332,"s":"Chris Pine, Ben Foster","y":2016},{"i":{"height":755,"imageUrl":"https://m.media-amazon.com/images/M/MV5BMjM5ODQ5Nzc3OF5BMl5BanBnXkFtZTgwOTQzMzM4NjE@.V1.jpg","width":509}

then no need to json_encode buddy.

$data = $response
return view('view_path',compact('data'));

Answer

Solution:

This is a just demonstration code but you should do something like this.

This is not part of your solution (ignore it) but I wanted to show how I tested it. I have put your response data in my test.json file.

$response = file_get_contents(public_path('json/test.json'));

$object = json_decode($response);

$array = collect($object)->toArray(); // Not needed if you get an array type in $object

Solution

foreach ($array as $data) {
    $data = collect($data)->toArray(); // Not needed if you get an array type in $data
    foreach ($data as $items) {
        $items = collect($items)->toArray(); // Not needed if you get an array type in $items
        foreach ($items as $value) {
            $value = collect($value)->toArray(); // Not needed if you get an array type in $value

            $image = collect($value["i"])->toArray(); // Not needed if you get an array type in $value["i"]

            $imageUrl = $image["imageUrl"];
            $rank = $value["rank"];
        }
    }
}

Source