Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

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

Comments

No comments

Let's comment your feelings that are more than good

Qiita Advent Calendar is held!

Qiita Advent Calendar is an article posting event where you post articles by filling a calendar 🎅

Some calendars come with gifts and some gifts are drawn from all calendars 👀

Please tie the article to your calendar and let's enjoy Christmas together!

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?

Login to continue?

Login or Sign up with social account

Login or Sign up with your email address