Skip to content

[Bug]: Undefined index 0 when mapping $addressComponents #42

@joehensonn

Description

@joehensonn

What happened?

When selecting a result from the autocomplete field, if the first element of the address_components array in the response from Google has an empty or missing types key, an “Undefined index 0” error occurs.

This happens because line 150 of getFormattedApiResults() in the HasGooglePlaceApi.php trait assumes that $item['types'][0] will always exist, but it doesn’t handle cases where the types array is empty. This is quite a common occurrence.

How to reproduce the bug

When using the GoogleAutocomplete field with no options configured, selecting a result from the autocomplete field that has an empty types array in its address_components causes an error.

For example, searching for The Royal Palace and selecting the top result in Stockholm triggers this issue.

  • Without placesApiNew(), this results in Undefined index 0
  • With placesApiNew() enabled, it instead results in: Undefined array key "types"

Package Version

1.0.13

PHP Version

8.3

Laravel Version

12.0

Which operating systems does with happen with?

macOS

Notes

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions