Undefined index:Title laravel 6.0

问题描述 投票:0回答:1

试图将数组的项目保存到数据库时出现上述错误

Am使用函数索引从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 [▶]
laravel guzzle omdbapi
1个回答
0
投票

更改此

collect($ movies ['Search']);

收件人

$ movies = collect($ movies ['Search']);

© www.soinside.com 2019 - 2024. All rights reserved.