0

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().

1

2 Answers 2

1

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')
Sign up to request clarification or add additional context in comments.

Comments

0

You may use toArray() helper

The toArray method converts the collection into a plain PHP array. If the collection's values are Eloquent models, the models will also be converted to arrays

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

/*
    [
        ['column1' => 'value1', 'column2' => 'value2', ...],
        ['column1' => 'value1', 'column2' => 'value2', ...],
        ['column1' => 'value1', 'column2' => 'value2', ...],
        ...
    ]
*/

Then you may use array_column() on $readings array

$pluckedValues = array_column($readings, 'chosen_column');

print_r($pluckedValues);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.