JaChNo
JaChNo

Reputation: 1563

Getting data from Laravel Collection

I realize this is a dumb question, but I can't find an answer for it. I am missing some understanding around how collections work

I am querying a google places API for information about a location

 $response = GooglePlaces::textSearch('10 Downing Street', $optionnalParameters);

This works and gives me a response, which I can var_export:

   Illuminate\Support\Collection::__set_state(array(
   'items' => 
  array (
    'html_attributions' => 
    array (
    ),
    'results' => 
    Illuminate\Support\Collection::__set_state(array(
       'items' => 
      array (
        0 => 
        array (
          'formatted_address' => '10 Downing St, London SW1A 2AA, United Kingdom',
          'geometry' => 
          array (
            'location' => 
            array (
              'lat' => 51.503363499999999,
              'lng' => -0.12762480000000001,
            ),
            'viewport' => 
            array (
              'northeast' => 
              array (
                'lat' => 51.503432400000001,
                'lng' => -0.12551999999999999,
              ),
              'southwest' => 
              array (
                'lat' => 51.503156800000014,
                'lng' => -0.12832640000000001,
              ),
            ),
          ),
          'icon' => 'https://maps.gstatic.com/mapfiles/place_api/icons/civic_building-71.png',
          'id' => 'ffd85a3e543406e34703ee947c73ef54f5e167fe',
          'name' => '10 Downing Street',
          'photos' => 
          array (
            0 => 
            array (
              'height' => 2534,
              'html_attributions' => 
              array (
                0 => '<a href="https://maps.google.com/maps/contrib/117230111047681784763/photos">Christopher Chan</a>',
              ),
              'photo_reference' => 'CoQBdwAAAHdqDNRUhalI7Ko_YXqckWM5M7I1IeD0xXOvjnxruS4BYCFnt99lCEy5xQJh7XtTvGTfZbKlnVhbxaJ_OloLxaPyoInqIpgRY-3LyB3Q70tDX3izeraFEM4Bw-ExmRzz6h18iMQlKb0DoDXnW26uO4RR-7YFjPNi6M0y5D7cmrl9EhAmI7GerM-TXpKD3BVVTtOWGhQZotS0ZNI2nK9G7jXxDEyvoQ5IxQ',
              'width' => 3801,
            ),
          ),
          'place_id' => 'ChIJRxzRQcUEdkgRGVaKyzmkgvg',
          'rating' => 3.3999999999999999,
          'reference' => 'CmRSAAAAh-Drhh9G_EW3azxZSikW_jR-ZjI2lhZTw6MfWqh9EiTaCEy4uW2okv_g6QfaKoupoeDu3DpfS6MjIvUvp6cc2uAoLlyH9NbkMrnQg9q3ED3R91OejmAScjRhe8G47kJ1EhD_oYVFakm4n6I27j-8iN6oGhQVH6rv1t6uGQBVVOqy1fCAB17JOg',
          'types' => 
          array (
            0 => 'point_of_interest',
            1 => 'establishment',
          ),
        ),
      ),
    )),
    'status' => 'OK',
  ),
))

I want to get the formatted_address element from the collection. it appears under the results element, that in turn has a collection under it with elements Items,0,formatted_address.

when i start trying to 'walk down' to the element i require I get index not found for Items var_export($response['results']['items']);

so essentially, how am I supposed to interact with collections? i have tried google but most the links tell me about the extra functions that collections provide and not how to get data from it

any help greatly appreciated.

Upvotes: 0

Views: 9724

Answers (1)

Gijs de Jong
Gijs de Jong

Reputation: 1027

I think https://laravel.com/docs/5.3/collections#method-all explains what you are missing. You want to call

->all()

on your Collection to return the underlying array represented by the collection. Than you can access fields in the returned array.

Upvotes: 1

Related Questions