3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravel wasChangedの検証

Posted at

isDirty() vs wasChanged() – ララジャパン

この記事読んで、これはwasChangedだけでいいのでは?と思ったので検証

>>> $group = App\Group::first();
=> App\Group {#3333
     id: 10,
     name: "Aグループ",
     mailaddress: "hoge@example.com",
     prefectures_id: 13,
     city: "千代田区",
     address: "大手町一丁目",
     telephone: "03XXXXXXXX",
     created_at: "2019-01-09 12:09:56",
     updated_at: "2019-06-17 07:58:21",
   }

値を変更してみる

>>> $group->name = "hoge"
=> "hoge"
>>> $group->isDirty("name")
=> true
>>> $group->wasChanged("name")
=> false

既に期待値と違う。これではwasChangedisDirtyの代わりに使うことはできない・・・
wasだし仕方ない。

saveしてみる

>>> $group->save()
=> true
>>> $group->isDirty("name")
=> false
>>> $group->wasChanged("name")
=> true
>>> $group->getChanges()
=> [
     "name" => "hoge",
     "updated_at" => "2019-07-05 18:10:02",
   ]

変更前のデータは残ってないんだな。

以前試した時の記事

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?