dtcyv3985 2017-05-18 12:07
浏览 39
已采纳

如何从Laravel 5.4上的tmdb API获取发布日期?

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\hallofsoundesources\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 ▶}
  ]
}
  • 写回答

1条回答 默认 最新

  • dongtuliao6760 2017-05-18 13:16
    关注

    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') !!}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?