Search code examples
phpapilaravel-5.4themoviedb-api

How to get Release Date from tmdb API on Laravel 5.4?


I am using Laravel PHP Framework, I'd like to use the tmdb package.

I can see release date in json. Here is screenshot

http://prntscr.com/f98ydy

But I want this in my view like I can get title with code:

{!! $movie->getTitle() !!}

How to get release date? I also tried with that code

{!! $movie->getReleaseDate() !!}

But showing me this error

Object of class DateTime could not be converted to string (View: C:\xampp\htdocs\hallofsound\resources\views\welcome.blade.php)

Here is my full Json output

   ResultCollection {#1811 ▼
  -page: 1
  -totalPages: 36
  -totalResults: 702
  #data: array:20 [▼
    "000000004255a5960000000012a0a3a3" => Movie {#1816 ▼
      -adult: false
      -backdropPath: "/aJn9XeesqsrSLKcHfHP4u5985hn.jpg"
      -backdrop: BackdropImage {#1327 ▶}
      -belongsToCollection: null
      -budget: null
      -genres: Genres {#1832 ▶}
      -homepage: null
      -id: 283995
      -imdbId: null
      -originalTitle: "Guardians of the Galaxy Vol. 2"
      -originalLanguage: "en"
      -overview: "The Guardians must fight to keep their newfound family together as they unravel the mysteries of Peter Quill's true parentage."
      -popularity: 115.058584
      -poster: PosterImage {#1837 ▶}
      -posterPath: "/y4MBh0EjBlMuOzv9axM4qJlmhzz.jpg"
      -productionCompanies: GenericCollection {#1331 ▶}
      -productionCountries: GenericCollection {#1830 ▶}
      -releaseDate: DateTime {#1840 ▶}
      -revenue: null
      -runtime: null
      -spokenLanguages: GenericCollection {#1828 ▶}
      -status: null
      -tagline: null
      -title: "Guardians of the Galaxy Vol. 2"
      -voteAverage: 7.6
      -voteCount: 1539
      #alternativeTitles: GenericCollection {#1826 ▶}
      #changes: GenericCollection {#1829 ▶}
      #credits: CreditsCollection {#1821 ▶}
      #images: Images {#1319 ▶}
      #keywords: GenericCollection {#1824 ▶}
      #lists: GenericCollection {#1820 ▶}
      #releases: GenericCollection {#1316 ▶}
      #release_dates: GenericCollection {#1822 ▶}
      #similar: GenericCollection {#1819 ▶}
      #translations: GenericCollection {#1825 ▶}
      #reviews: null
      #videos: Videos {#1831 ▶}
    }
    "000000004255a5a00000000012a0a3a3" => Movie {#1838 ▶}
    "000000004255a5c90000000012a0a3a3" => Movie {#1863 ▶}
    "000000004255a5ee0000000012a0a3a3" => Movie {#1888 ▶}
    "000000004255a5f70000000012a0a3a3" => Movie {#1913 ▶}
    "000000004255a51f0000000012a0a3a3" => Movie {#1937 ▶}
    "000000004255a5240000000012a0a3a3" => Movie {#1962 ▶}
    "000000004255a54d0000000012a0a3a3" => Movie {#1987 ▶}
    "000000004255a5550000000012a0a3a3" => Movie {#2011 ▶}
    "000000004255a57d0000000012a0a3a3" => Movie {#2035 ▶}
    "000000004255aa840000000012a0a3a3" => Movie {#2058 ▶}
    "000000004255aaac0000000012a0a3a3" => Movie {#2082 ▶}
    "000000004255aab50000000012a0a3a3" => Movie {#2107 ▶}
    "000000004255aadb0000000012a0a3a3" => Movie {#2133 ▶}
    "000000004255aae00000000012a0a3a3" => Movie {#2158 ▶}
    "000000004255aa080000000012a0a3a3" => Movie {#2182 ▶}
    "000000004255aa110000000012a0a3a3" => Movie {#2207 ▶}
    "000000004255aa360000000012a0a3a3" => Movie {#2232 ▶}
    "000000004255aa5f0000000012a0a3a3" => Movie {#2257 ▶}
    "000000004255aa640000000012a0a3a3" => Movie {#2282 ▶}
  ]
}

Solution

  • The JSON response is a collection. So you need to loop through it. See here

    So maybe something like this:

    foreach($movies as $movie)
    {
        var_dump($movie->getTitle());
    }
    

    Also the method getReleaseDate will return a DateTime object. To format it, you can probably do something like this:

    $date   = new DateTime($movie->getReleaseDate());
    $result = $date->format('Y-m-d H:i:s');
    

    or in Laravel, you can do this:

    {!! $movie->getReleaseDate()->format('Y-m-d H:i:s') !!}