Reputation: 12025
First, I create collection from array:
$bank_center = collect(array("amount" => null, "mfo" => null, "name" => null));
Then I try to get value by key:
dd($bank_center->name);
Dump is:
Collection {#562 ▼
#items: array:3 [▼
"amount" => null
"mfo" => null
"name" => null
]
}
Upvotes: 0
Views: 202
Reputation:
To retrieve element by name from collection you can use get method, it returns the item at a given key. If the key does not exist, null is returned:
$collection = collect(['name' => 'bruno', 'framework' => 'laravel']);
$value = $collection->get('name');
// bruno
Upvotes: 1
Reputation: 8750
In your particular case, the following would just work:
$bank_center['name'];
I am not sure why you want to wrap it as an object, but if you still wish to do it, I'd recommend you take a look at Fluent.
$bank_center = new \Illuminate\Support\Fluent(array("amount" => 'test', "mfo" => 'test2', "name" => 'test3'));
dd($bank_center->name); // test3
Upvotes: 2
Reputation: 10086
You should use square brackets to access item from such collection:
$bank_center['name']
Upvotes: 1