As others have pointed out, you'd have to first turn the collection into an array and then modify the key.
$array = $tttt->toArray();
$array[0]['same_manual_ticket_group'][0]['id']='FFFF';
$array[1]['same_manual_ticket_group'][0]['id']='BBBB';
This happens probably because they are referencing the same model, so they point to the same data.
When you dump the variable, you can see a number next to each class. For example
Collection{#2480
If what you are trying to modify shares numbers, then it's referencing the same object
An easy way to see this is by running the following in php artisan tinker
>>> $mt = new App\ManualTicket
=> App\ManualTicket {#1}
>>> $collection = collect([$mt, $mt]);
=> Illuminate\Support\Collection {#2
all: [
App\ManualTicket{#1},
App\ManualTicket{#1},
],
}
>>> $collection->get(0)->something = 'something'; // update only the first one
=> "something"
>>> $collection // show collection again. Surprise, both values updated.
=> Illuminate\Support\Collection {#2
all: [
App\ManualTicket{#1
something: "something",
},
App\ManualTicket{#1
something: "something",
},
],
}
same reference number, both values update. When transforming to an array, you don't have to deal with that. Another possibility (if you know exactly which values to change) is to clone
the objects. Here's how that would look in our php artisan tinker
example
>>> $clone = clone $collection->get(0);
=> App\ManualTicket{#3
something: "something",
},
>>> $collection->put(0, $clone);
=> Illuminate\Support\Collection {#2
all: [
App\ManualTicket{#3
something: "something",
},
App\ManualTicket{#1
something: "something",
},
],
}
>>> $collection->get(0)->something = 'something else';
=> "something else"
>>> $collection // show collection again. Surprise, now both values are different.
=> Illuminate\Support\Collection {#2
all: [
App\ManualTicket{#3
something: "something else",
},
App\ManualTicket{#1
something: "something",
},
],
}
Applied to this case:
$cloned = clone $tttt->get(0)->same_manual_ticket_group->get(0);
$cloned->id ='FFFF';
$tttt->get(0)->same_manual_ticket_group->put(0, $cloned);
// no need to clone the second one, it's already a different reference.
$tttt->get(1)->same_manual_ticket_group->get(0)->id ='FFFF';