【问题标题】:Undefined index: Title laravel 6.0未定义索引:标题 laravel 6.0
【发布时间】:2020-02-16 21:40:19
【问题描述】:

我正在尝试将数组的项目保存到数据库中,但出现上述错误

我正在使用函数索引从 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;
    }

然后我使用函数 store 存储它们

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 [▶]

【问题讨论】:

  • 你能显示被注释掉的行dd($movie);的输出吗
  • @Tony 用输出更新了我的代码
  • 我能看到 dd($this->index) 吗?

标签: laravel guzzle omdbapi


【解决方案1】:

改变这个

收集($movies['搜索']);

到

$movies = collect($movies['搜索']);

【讨论】:

    猜你喜欢
    • 2018-08-02
    • 2012-08-24
    • 2015-03-23
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    • 2021-10-06
    相关资源
    最近更新 更多