I want to fetch some data from an API and then pass it to a blade view.
but when i try to pass the data to the blade view i get an error:
Undefined variable. $result is undefined
The function I'm using in my Controller is:
public function getAll(Request $request){
$userInput = $request->input();
$response = Http::withHeaders(
[
"x-rapidapi-host"=> "xxxxxxxx",
"x-rapidapi-key"=> "xxxxxxxxx",
]
)->get("https://imdb8.p.rapidapi.com/auto-complete?q=",$userInput);
return view('list', ['result' => $response]);
}
and my blade file is:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>movies</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>
<body>
<div class="m-2">
<form action="/getList" method="get">
<input class="form-control w-50 m-2" type="text" name="q" id="showName">
<input class="btn btn-primary m-2" type="submit" value="Search...">
</form>
</div>
{{$result['l']}}
</body>
</html>
I have tried to use foreach to loop through the array in the blade view but i had the same error so i changed the code to the code above but still having the same error!
and in my controller i have tried to use toArray(); with $response after reading the Laravel documentation for a while but still got the same error.
how can i pass an array of objects from the controller to the blade view?