未定义索引:标题 laravel 6.0
Undefined index: Title laravel 6.0
我正在尝试将数组的项目保存到数据库,但出现上述错误
正在使用函数 index
从 omdb api 获取数组
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 [▶]
改变这个
收集($电影['Search']);
到
$movies = 收集($movies['Search']);
我正在尝试将数组的项目保存到数据库,但出现上述错误
正在使用函数 index
从 omdb api 获取数组 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 [▶]
改变这个
收集($电影['Search']);
到
$movies = 收集($movies['Search']);