簡體   English   中英

如何在Laravel 5.2中訪問集合中的數據

[英]How to access data from collection in laravel 5.2

我有這種形式的結果

RowCollection {#780 ▼
  #heading: array:7 [▶]
  #title: "Sheet1"
  #items: array:3 [▶]
}

我必須訪問標題,但是當我使用foreach循環時

foreach( $data as $key => $value){
echo $value;
}

它打印出項目數組的值。那么如何訪問標題數組?

根據我在laravel及其var_dumper中的經驗,可以在dd()輸出中使用#號簽名的項目可以作為具有以下模式的方法進行訪問:

get{ItemStudlyCaseName}()

例如getHeading() getTitle() getItems()

並且帶有+號簽名的項目可以作為屬性訪問。

完整說明

dd() var_dumper輸出中,有三個符號:

#受保護的財產

+公共財產

-私有財產

可以使用$object->get{PropertyStudlyCaseName}()模式的getter方法訪問受保護的屬性。

可以直接訪問公共屬性。 $object->propertyName

無法訪問私有財產。

例如在請求對象中:

Request {#38 ▼
  #json: null
  #convertedFiles: null
  #userResolver: Closure {#142 ▶}
  #routeResolver: Closure {#143 ▶}
  +attributes: ParameterBag {#40 ▶}
  +request: ParameterBag {#46 ▶}
  +query: ParameterBag {#46 ▶}
  +server: ServerBag {#42 ▶}
  +files: FileBag {#43 ▶}
  +cookies: ParameterBag {#41 ▶}
  +headers: HeaderBag {#44 ▶}
  #content: null
  #languages: null
  #charsets: null
  #encodings: null
  #acceptableContentTypes: null
  #pathInfo: "/"
  #requestUri: "/"
  #baseUrl: ""
  #basePath: null
  #method: "GET"
  #format: null
  #session: Store {#185 ▶}
  #locale: null
  #defaultLocale: "en"
  -isHostValid: true
  -isClientIpsValid: true
  -isForwardedValid: true
  basePath: ""
  format: "html"
}

例如

#受保護的屬性: $request->getDefaultLocale()

+公共財產: $request->attributes

-私有屬性: $request->isHostValid =>返回null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM