Laravel获取收集数据

问题描述 投票:0回答:3

下面是我的数组结果。我试图访问sublocality数据,但我一直运行错误:

不能使用类型的对象作为数组

我使用$result[$some_imdex]->subLocality这没用。

array:5 [▼
    0 => GoogleAddress {#204 ▼
    -id: "ChIJGzuDx6X1OxARcJhhmLrPLPs"
    -locationType: "ROOFTOP"
    -resultType: array:1 [▶]
    -formattedAddress: "27b Bolaji Street, Maroko, Lagos, Nigeria"
    -streetAddress: null
    -intersection: null
    -political: "Nigeria"
    -colloquialArea: null
    -ward: null
    -neighborhood: "Maroko"
    -premise: null
    -subpremise: null
    -naturalFeature: null
    -airport: null
    -park: null
    -pointOfInterest: null
    -establishment: null
    -subLocalityLevels: AdminLevelCollection {#205 ▶}
    -coordinates: Coordinates {#207 ▶}
    -bounds: Bounds {#208 ▶}
    -streetNumber: "27b"
    -streetName: "Adewale Kolawole Crescent"
    -subLocality: "Eti-Osa"
    -locality: "Lagos"
    -postalCode: null
    -adminLevels: AdminLevelCollection {#209 ▶}
    -country: Country {#212 ▶}
    -timezone: null
    -providedBy: "google_maps"
  }
  1 => GoogleAddress {#213 ▶}
  2 => GoogleAddress {#221 ▶}
  3 => GoogleAddress {#228 ▶}
  4 => GoogleAddress {#236 ▶}
]
laravel laravel-5 geocode
3个回答
0
投票

它包裹在一个数组上的对象响应!

所以你试图访问该数组中的一个对象试图通过索引访问,因此你得到了这个错误。

你能做什么:将数组转换为集合,然后映射到该集合:

collect($results)->filter($result){

    return $result->subLocality === 'some value';

});

qazxsw poi方法将过滤集合并返回符合条件的元素。

或者,如果您想返回一些特定数据,请使用filter()

map()

collect($results)->map($result){ return [ '0'=>$result->subLocality, '1'=>$result->someIndex, ]; }); 将返回给定集合中的变换数组。更多阅读map()


0
投票

因为这是一个集合,所以不能使用数组索引来获取特定模型。您可以使用集合方法来访问该模型,IE:

Laravel Collection

0
投票
  • $result->where('id', $some_id)->first()->subLocality 私人
  • -公众
  • +受到保护

您不能直接在课堂外访问您想要的酒店,而不是公共酒店。

扩展#GoogleAddress类具有访问所有这些字段的方法(getters)。

Address

无论您是拥有集合还是数组,或者您拥有什么......您都无法直接访问该类的这些属性。

您还可以选择在其中一个实例上调用$addressObject->getSubLocality(); 来获取关联数组,这可能会更容易,具体取决于您正在执行的操作。

© www.soinside.com 2019 - 2024. All rights reserved.