未定义索引:标题laravel 6.0
我试图将数组中的项保存到db中,但收到上述错误 使用函数索引从omdb api获取数组未定义索引:标题laravel 6.0,laravel,guzzle,omdbapi,Laravel,Guzzle,Omdbapi,我试图将数组中的项保存到db中,但收到上述错误 使用函数索引从omdb api获取数组 public function index() { $client = new Client(); $uri = 'http://www.omdbapi.com/?s=water&apiKey='; $header = ['headers' => ['X-Auth-Token' => 'My-Token']]; $r
public function index()
{
$client = new Client();
$uri = 'http://www.omdbapi.com/?s=water&apiKey=';
$header = ['headers' => ['X-Auth-Token' => 'My-Token']];
$res = $client->get($uri, $header);
$data = json_decode($res->getBody()->getContents(), true);
return $data;
}
然后,我使用函数存储来存储它们
public function store(Request $request)
{
$movies = $this->index();
// dd($this->index());
collect($movies['Search']);
// dd($movies);
foreach($movies as $movie) {
// dd($movie);
Movie::create([
'title' => $movie['Title'],
'year' =>$movie['Year'],
'type' =>$movie['Type'],
'cover_photo' => $movie['Poster'],
]);
}
}
This is the output of dd($movie)
array:10 [▼
0 => array:5 [▼
"Title" => "The Shape of Water"
"Year" => "2017"
"imdbID" => "tt5580390"
"Type" => "movie"
"Poster" => "https://m.media-amazon.com/images/M/MV5BNGNiNWQ5M2MtNGI0OC00MDA2LWI5NzEtMmZiYjVjMDEyOWYzXkEyXkFqcGdeQXVyMjM4NTM5NDY@._V1_SX300.jpg"
]
1 => array:5 [▶]
2 => array:5 [▶]
这是dd$This->index的输出
array:3 [▼
"Search" => array:10 [▼
0 => array:5 [▶]
1 => array:5 [▶]
2 => array:5 [▶]
3 => array:5 [▶]
4 => array:5 [▶]
5 => array:5 [▶]
改变这个
收集$movies['Search']
到
$movies=收集$movies['Search'] 您能否显示注释掉的dd$电影行的输出@Tony用输出更新了我的代码我能看到dd$this->index吗?