Skip to content

Instantly share code, notes, and snippets.

@DavidKloucek
Last active June 25, 2023 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidKloucek/a0f60f14aea0c450a5c5a7874007950b to your computer and use it in GitHub Desktop.
Save DavidKloucek/a0f60f14aea0c450a5c5a7874007950b to your computer and use it in GitHub Desktop.
Laravel Eloquent problem
<?php
public function testIdentityMap()
{
Comment::query()->where(['id' => 1])->update(['content' => 'a']); // set default value for the first comment
$user = User::query()->find(1);
$user->name = 'David '.date('s');
$user->save();
$user2 = User::query()->find(1);
dump($user2->name === $user->name); // true
$firstComment = Comment::query()->find(1);
foreach ($user->comments as $c) {
if ($c->id === 1) {
dump($c === $firstComment); // false
$c->content = 'aaa';
$c->save();
dump($c->content); // "aaa"
dump($firstComment->content); // "a" .. wrong value
$firstComment->refresh(); // fix it
dump($firstComment->content); // "aaa"
dump($firstComment->content === $c->content); // finally true!
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment