I'm trying to keep the original data in a temporary variable called $copyItems
, but whenever I change the value of the original variable $items
it also changes in the copied variable.
Take the following example:
$items = \App\Models\Customers::with('Account')->get();
$copyItems = $items; // Should hold all the original $items values no matter the changes
// Prints "Stackoverflow"
echo $copyItems[0]->Account->name;
$items[0]->Account->name = 'John';
// Prints "John" and should print "Stackoverflow"
echo $copyItems[0]->Account->name;
How d'hell is this happening? I'm not using variable references &$items
why is my temporary variable being changed?
With the clone $items
approach the result is the same.