Jump to content

Weird laravel error


SamC
 Share

Recommended Posts

So I know this is a PW forum but this is the off topic section so hopefully is ok. I've been learning Laravel for the past couple of weeks and hoping someone can help me out with this, it's driving me nuts! I built a small movie app using a 'diy MVC framework' from a Udemy course which was awesome. Now I'm porting it over to Laravel, and using Guzzle to query TMDB API to see how I can better organise my code (and learn Laravel at the same time). 

I'm getting an error on the page:

http://movies.local/movies/list?name=denzel+washington

...which is:

Undefined index: release_date (View: /Users/sam/www/movies.local/resources/views/movies.blade.php)

...pointing at this (on the laravel error page):

<p>Release date: <?php echo e($movie['release_date']); ?></p>

I'm outputting from my view thus (inside a loop, $movie represents a single movie in an array):

<pre>
    <?php var_dump($movie); ?>
</pre>

<h3>{{ $movie['title'] }}</h3> <?php // THIS WORKS ?>
<p>ID: {{ $movie['id'] }}</p> <?php // AND THIS... ?>
<p>Release date: {{ $movie['release_date'] }}</p> <?php // THIS LINE IS BAD FOR SOME REASON... ?>
<p>{{ $movie['overview'] }}</p> <?php // AND THIS... ?>

... when I remove the 'Release date' line, the page loads ok and the var_dump() spits out:

array(17) {
  ["character"]=>
  string(13) "Robert McCall"
  ["credit_id"]=>
  string(24) "5588a41ec3a3683d500009cb"
  ["poster_path"]=>
  string(32) "/cQvc9N6JiMVKqol3wcYrGshsIdZ.jpg"
  ["id"]=>
  int(345887)
  ["video"]=>
  bool(false)
  ["vote_count"]=>
  int(1221)
  ["adult"]=>
  bool(false)
  ["backdrop_path"]=>
  string(32) "/z7noaCJ4KtmhwHw7QcNtnMMo4Qy.jpg"
  ["genre_ids"]=>
  array(3) {
    [0]=>
    int(28)
    [1]=>
    int(80)
    [2]=>
    int(53)
  }
  ["original_language"]=>
  string(2) "en"
  ["original_title"]=>
  string(15) "The Equalizer 2"
  ["popularity"]=>
  float(92.746)
  ["title"]=>
  string(15) "The Equalizer 2"
  ["vote_average"]=>
  float(6.2)
  ["overview"]=>
  string(183) "Robert McCall, who serves an unflinching justice for the exploited and oppressed, embarks on a relentless, globe-trotting quest for vengeance when a long-time girl friend is murdered."
  ["release_date"]=>
  string(10) "2018-07-20"
  ["custom_image_path"]=>
  string(63) "http://image.tmdb.org/t/p/w185/cQvc9N6JiMVKqol3wcYrGshsIdZ.jpg"
}

...so it can be seen quite clearly that the key 'release_date' does in fact exist. And the other keys work ok, and they're on the same level.

And it gets weirder, if I replace the Blade:

<p>Release date: {{ $movie['release_date'] }}</p>

...with

<p>Release date: <?= (isset($movie['release_date'])) ? $movie['release_date'] : 'No date'; ?></p>

...then everything works fine, the date is output as it should be. But surely I don't need to check each key? I know it exists as the movies listed inside this loop are one that have a release date, it's been checked earlier in the process.

Like, if I chuck in:

<p>Rating: {{ $movie['vote_average'] }}</p>
<p>Credit ID: {{ $movie['credit_id'] }}</p>

...then they display ok too.

What's so special about this 'release_date' key? Or do I have a really huge face palm moment typo that I just can't see?

Thanks for any advice.

ps the actual page looks like this (when I use the plain PHP code with isset() check to output the release date):

78431542_ScreenShot2018-12-11at19_09_42.thumb.png.fffe5ff6aed3081479c9654fdec56142.png

Link to comment
Share on other sites

1 hour ago, LostKobrakai said:

This seems to be an index page. Are you sure the error happens on the first item of the collection? In other words did you check all items for that key as you‘ve only posted one here.

Doh! You're totally right. Just found one right at the end:

[45] => Array
(
    [character] => Humpty Dumpty
    [credit_id] => 52fe4cbcc3a36847f823c93b
    [vote_count] => 2
    [video] => 
    [popularity] => 1.49
    [original_language] => en
    [vote_average] => 0.5
    [id] => 167867
    [title] => Happily Ever After: Fairy Tales for Every Child Mother goose big book of Rhymes
    [original_title] => Happily Ever After: Fairy Tales for Every Child Mother goose big book of Rhymes
    [backdrop_path] => 
    [genre_ids] => Array
        (
        )

    [adult] => 
    [overview] => Fairy tales have always been a source of family pleasure, for parents and children alike. Now, some of the best-loved classics are being re-told in this vibrantly original and musical animated series, Happily Ever After: Fairy Tales For Every Child. Each tale features a hip, contemporary all-star cast of voices and all-new songs the entire family will enjoy! Classic fairy tales become fresh and fun, told as if for the very first time. So share the magic and wonder with your children as we invite you to enter the wonder-filled world of Happily Ever After. Includes the following Fairy Tales:  Mother Goose, A Rappin' & Rhymin' Special  Pinocchio  The Golden Goose  The Pied Piper
    [poster_path] => /rSJSZiwyAVl165lmRXgNoUp4VWy.jpg
    [custom_image_path] => http://image.tmdb.org/t/p/w185/rSJSZiwyAVl165lmRXgNoUp4VWy.jpg
)

How this works is that I query the TMDB with a name (from the form), from that, you get the actor ID, from there the ID is used to get details about the actors, and everything they've starred in/directed etc.... there's no database involved at all (although in the future, I'd like it to build dynamically as the searches happen to cut down on API requests. You can only query 4 times a second at TMDB).

The results from TMDB don't seem to be very consistent. With this 'release_date' key, it's either:

1) Exists and has value
2) Exists but no value
3) Doesn't exit at all

I think, based on what I've already said, that I need to check values exist first before doing anything. Thanks @LostKobrakai

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...