Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thebigk's avatar
Level 13

Collection Basics - How do I access elements of collection with `->` ?

I've been using eloquent collections for long time now; but this is first time I ever had to create my own custom collection from scratch.

$collection = collect(['one' => 1, 'two' => 2, 'three' => 3]);

Now I want to be able to say $collection->one and it should throw 1, but it doesn't. Instead, I've to write $collection['one'] to get the desired value.

What am I doing wrong?

0 likes
10 replies
tykus's avatar

A Collection does not expose the items keys as its Object properties; it implements ArrayAccess for this reason

1 like
thebigk's avatar
Level 13

@tykus - thank you for your response, but could you please explain this a bit in more detail? How do I go about achieving the desired result?

Amaury's avatar

@thebigk for you information:

In Laravel, you have

  1. Illuminate\Support\Collection that's what is returned by the collect() method
  2. Illuminate\Database\Eloquent\Collection that's what is returned when you query database

With Illuminate\Database\Eloquent\Collection, you can use the arrow syntax because it is build as.

thebigk's avatar
Level 13

Well, I of course don't need collection; but I'm used to playing with the collections that eloquent returns. I thought I could simply create a new collection with the collect() helper and it'd work as expected. I was surprised that it didn't.

I'm curious what's the rationale behind creating collections? Yes, it does make the syntax very expressive; but is that it?

Addendum:

$collection = new Illuminate\Database\Eloquent\Collection(
  [
    'one' => 1,
    'two' => 2,
    'three' => 3
  ]
);

$collection->one;

throws error

Agelios's avatar

You can do something like this:

1.Create your own class and extend collection functionality and add functional which you want

class MyCollection extends \Illuminate\Support\Collection
{
    public function __get($key)
    {
        return $this[$key];
    }
}

2.Profit :)

$test = new MyCollection(['one' => 1]);
dd($test->one);
// 1
1 like
Snapey's avatar

Collection is an array on steroids. It can hold an array of objects, but you cannot access the items in the collection using named properties

Sinnbeck's avatar

Imagine a collection like a zero indexed array

Array wouldn't work either

$array = [
  [
    'one' => 1,
    'two' => 2,
    'three' => 3
  ]
];

$array['one']; //throws error as well
$array[0]['one']; //works

//last one is like this 
$collection = collect(
  (object) [
    'one' => 1,
    'two' => 2,
    'three' => 3
  ]
);

$collection->first()->one; //works
$collection[0]->one; //works
jlrdw's avatar
jlrdw
Best Answer
Level 75

If you have:

$employees = collect([
    ['id' => 4, 'email' => null, 'position' => 'Developer'],
    ['id' => 7, 'email' => '[email protected]', 'position' => 'Designer'],
    ['id' => 9, 'email' => '[email protected]', 'position' => 'Developer'],
    ['id' => 2, 'email' => '[email protected]', 'position' => 'boss'],
 ]);

Works:

echo $employees[0]["id"];

Don't work

echo $employees[0]->id;

If you need objects

$newemployees = $employees->toJson();
$newemp = json_decode($newemployees);

then

echo $newemp[0]->id;    //// 4

Gives:

Array
(
    [0] => stdClass Object
        (
            [id] => 4
            [email] => 
            [position] => Developer
        )

    [1] => stdClass Object
        (
            [id] => 7
            [email] => [email protected]
            [position] => Designer
        )

    [2] => stdClass Object
        (
            [id] => 9
            [email] => [email protected]
            [position] => Developer
        )

    [3] => stdClass Object
        (
            [id] => 2
            [email] => [email protected]
            [position] => boss
        )

)

@thebigk the trick here is to play around and experiment with this stuff in order to learn it.

1 like

Please or to participate in this conversation.