Ad

Laravel Access Values From Object In Object

In one of the controller function

$readings = Reading::orderBy('reading_date_time', 'DESC')->get();
dd($readings);

This gives

Illuminate\Database\Eloquent\Collection {#1883
  #items: array:160 [
    0 => App\Reading {#1722
      #fillable: array:6 [ …6]
      #connection: "mysql"
      #table: "readings"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:8 [ …8]
      #original: array:8 [ …8]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [ …1]
    }...

How can I access the actual values of readings table. And use them for operations like array_column().

Ad

Answer

This function already give you the result, and the result is collection.

You can call the attribute by single object in collection:

$readings->first()->reading_date_time;

and you can use method-pluck to get the attribute values from collection, it just like array_column:

$readings->pluck('reading_date_time');

If you use array_column, you can use method-toArray to change collection to array:

array_column($readings->toArray(), 'reading_date_time')
Ad
source: stackoverflow.com
Ad