douxiza9868
2019-05-31 13:40
浏览 877
已采纳

如何从Laravel中的对象获取属性

I am beginner in Laravel. I use Laravel 5.8 in my project.

I have this object:

NominatimAddress {#1146 ▼
  -attribution: "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright"
  -category: "building"
  -displayName: "99, 9 Maja, Janowo, Olsztyn, powiat pomorski, pomorskie, "
  -osmType: "way"
  -osmId: 142789154
  -type: "yes"
  -coordinates: Coordinates {#1147 ▼
    -latitude: 51.51171143
    -longitude: 11.4023121
  }
  -bounds: Bounds {#1148 ▶}
  -adminLevels: AdminLevelCollection {#1149 ▶}
  -country: Country {#1152 ▶}
  -timezone: null
  -providedBy: "nominatim"
}

How can I get latitude and longitude from this object?

图片转代码服务由CSDN问答提供 功能建议

我是Laravel的初学者。 我在我的项目中使用Laravel 5.8。

我有这个对象:

  NominatimAddress {#1146▼
 -attribution:“Data  ©OpenStreetMap贡献者,ODbL 1.0。https://osm.org/copyright"
-category:“building”
 -displayName:“99,9 Maja,Janowo,Olsztyn,powiat pomorski,pomorskie,”
 -osmType  :“way”
 -osmId:142789154 
 -type:“yes”
 -coordinates:坐标{#1147▼
 -latitude:51.51171143 
 -longitude:11.4023121 
} 
 -bounds:Bounds {  #1148▶} 
 -adminLevels:AdminLevelCollection {#1149▶} 
 -country:Country {#1152▶} 
 -timezone:null 
 -providedBy:“nominatim”
} 
  <  / pre> 
 
 

如何从此对象获取纬度和经度?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doutouman6245 2019-05-31 13:59
    已采纳

    As I can see, you are using Geocoder library. So, NominatimAddress extends Address, which have following method:

    public function getCoordinates()
    {
        return $this->coordinates;
    }
    

    And as Bogdan commented, coordinates are incapsulated into Coordinates class, which has accessors for latitude and longitude.

    Then you can try:

    $lat = $object->getCoordinates()->getLatitude();
    $lon = $object->getCoordinates()->getLongitude();
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douxuexiao1748 2019-05-31 13:42

    Accessing attribute in Laravel Eloquent Model is pretty easy. It's same as accessing stdClass object's attributes.

    Let's say you have variable $object that holds your object. You will access longitude and latitude attributes with:

    $lat = $object->coordinates->latitude;
    
    评论
    解决 无用
    打赏 举报
  • dtu11716 2019-05-31 14:07
    $lat = $object->coordinates->latitude;
    $long = $object->coordinates->longitude;
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题