简体   繁体   中英

How to access data from collection in laravel 5.2

I have a result in this form

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

i have to access heading ,but when i use foreach loop

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

it print out the items array value.So how to access to heading array ?

Based on my experience in laravel and its var_dumper, items signed with # sign in dd() output can be accessed as methods with follows pattern :

get{ItemStudlyCaseName}()

eg getHeading() getTitle() getItems()

and items signed with + sign can be accessed as properties.

Complete Description

In dd() var_dumper output there is three sign :

# protected property

+ public property

- private property

protected properties can be accessed by getter methods with $object->get{PropertyStudlyCaseName}() pattern.

public properties can be accessed directly. $object->propertyName

private properties is not accessible.

For example in Request object:

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"
}

eg

# protected property : $request->getDefaultLocale()

+ public property : $request->attributes

- private property : $request->isHostValid => returns null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM