Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kadzo's avatar
Level 1

Illegal string offset 'Title' while saving api data to db laravel

Am trying to save data from an api to my database i have a function to fetch

public function index()
    {
        $client = new Client();
        $uri = 'http://www.omdbapi.com/?t=tree&apikey=';
        $header = ['headers' => ['X-Auth-Token' => 'My-Token']];
        $res = $client->get($uri, $header);
        $data = json_decode($res->getBody()->getContents(), true);
  return $data;
    }

And then a function to save in my db


public function store(Request $request)
    {
        $movies = $this->index();
//        dd($this->index());

        collect($movies);
             foreach($movies as $movie) {
//                dd($movie);
                Movie::create([
                    'title' => $movie['Title'],
                     'year' =>$movie['Year'],
                    'type' =>$movie['Type'],
                    'cover_photo' => $movie['Poster'],


                ]);

            }

    }

When trying to save i get the error above

0 likes
17 replies
Sinnbeck's avatar

Try this one the line below each

dd($movie);

Or post the output of this commented out line

dd($this->index());
kadzo's avatar
Level 1
array:25 [▼
  "Title" => "The Tree of Life"
  "Year" => "2011"
  "Rated" => "PG-13"
  "Released" => "17 May 2011"
  "Runtime" => "139 min"
  "Genre" => "Drama, Fantasy"
  "Director" => "Terrence Malick"
  "Writer" => "Terrence Malick"
  "Actors" => "Brad Pitt, Sean Penn, Jessica Chastain, Hunter McCracken"
  "Plot" => "The story of a family in Waco, Texas in 1956. The eldest son witnesses the loss of innocence and struggles with his parents' conflicting teachings."
  "Language" => "English"
  "Country" => "USA"
  "Awards" => "Nominated for 3 Oscars. Another 116 wins & 123 nominations."
  "Poster" => "https://m.media-amazon.com/images/M/MV5BMTMwNjQ0NjMzN15BMl5BanBnXkFtZTcwNjMxMTkyNA@@._V1_SX300.jpg"
  "Ratings" => array:3 [▶]
  "Metascore" => "85"
  "imdbRating" => "6.8"
  "imdbVotes" => "160,256"
  "imdbID" => "tt0478304"
  "Type" => "movie"
  "DVD" => "11 Oct 2011"
  "BoxOffice" => ",303,319"
  "Production" => "Fox Searchlight"
  "Website" => "N/A"
  "Response" => "True"
]
Sinnbeck's avatar

That means you are only getting one movie.


public function store(Request $request)
    {
        $movie = $this->index();
//        dd($this->index());

                Movie::create([
                    'title' => $movie['Title'],
                    'director' => $movie['Director'],
                    'release_date' => $movie['Released'],
                    'plot' => $movie['Plot'],
                    'actors' => $movie['Actors'],
                    'runtime' => $movie['Runtime'],
                    'genre' => $movie['Genre'],
                    'production' => $movie['Production'],
                    'writer' => $movie['Writer'],
                    'cover_image' => $movie['Poster'],
                    'language' => $movie['Language'],
                    'rated' => $movie['Rated'],

                ]);

            

    }
kadzo's avatar
Level 1

yes the endpoint returns only one movie is that the problem?

Sinnbeck's avatar

Try s instead of t

http://www.omdbapi.com/?s=tree&apikey=';
kadzo's avatar
Level 1

i get less fields when i use s

Sinnbeck's avatar

Well from reading the docs you can use either

  • t = (title) get first by title. Returns the full match for the first hit in the database (one movies)
  • s = (search) get alle movies that matches but with less details (multiple movies)

I cannot change how their api works

kadzo's avatar
Level 1

am getting undefined index title

Sinnbeck's avatar

Didn't we solve that already? Perhaps show your code and the response from the api :)

kadzo's avatar
Level 1

i changed my store function see the updated code

kadzo's avatar
Level 1

api response


"Search" => 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 [▼
      "Title" => "The Shape of Water"
      "Year" => "2017"
      "imdbID" => "tt5580390"
      "Type" => "movie"
      "Poster" => "https://m.media-amazon.com/images/M/MV5BNGNiNWQ5M2MtNGI0OC00MDA2LWI5NzEtMmZiYjVjMDEyOWYzXkEyXkFqcGdeQXVyMjM4NTM5NDY@._V1_SX300.jpg"
    ]
    2 => array:5 [▼
      "Title" => "Hell or High Water"
      "Year" => "2016"
      "imdbID" => "tt2582782"
      "Type" => "movie"
      "Poster" => "https://m.media-amazon.com/images/M/MV5BMTg4NDA1OTA5NF5BMl5BanBnXkFtZTgwMDQ2MDM5ODE@._V1_SX300.jpg"
    ]
Sinnbeck's avatar

There you go. The data is inside Search it would seem

public function store(Request $request)
    {
        $movies = $this->index();
//        dd($this->index());

        collect($movies['Search']);
             foreach($movies as $movie) {
//                dd($movie);
                Movie::create([
                    'title' => $movie['Title'],
                     'year' =>$movie['Year'],
                    'type' =>$movie['Type'],
                    'cover_photo' => $movie['Poster'],


                ]);

            }

    }
kadzo's avatar
Level 1

still getting the same error

Sinnbeck's avatar

Well you can debug to see what you are getting. Uncomment the dd($movie); and see what you get

Please or to participate in this conversation.