lundi 26 décembre 2016

(Laravel 5.3) How to add new key and value to existing sub array in existing Collection?

I am using Laravel 5.3.

existing Collection $a is

$a = collect(
[
 [
  'firstName' => 'John',
  'lastName' => 'Doe'
 ],
 ['firstName' => 'Mary',
  'lastName' => 'Jane'
 ]
]);

and desired result is as below:

$a = collect(
[
 [
  'firstName' => 'John',
  'lastName' => 'Doe',
  'occupation' => 'engineer'

 ],
 ['firstName' => 'Mary',
  'lastName' => 'Jane',
  'occupation' => 'accountant'
 ]
]);

I tried to test ->push(), ->put(), ->prepend() but no success. Please let me know the best way to do this.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire