LoginSignup
2

More than 5 years have passed since last update.

Laravelでテーブルにデータを保存する時に自動保存されるupdated_atの中身を調べた

Posted at

2つのテーブルに存在するレコードの更新日時を比較する処理を実装したくて、updated_atカラムの値を取得して比較しようとしていたのですが、うまくいかなかったのでdd()updated_atに入った値の中身を見てみました。

結論

・updated_atカラムに挿入された値はCarbonインスタンスだった
Carbonインスタンスなので、以下の記事にあるようにtimestamp等様々なアクセサが使えます。
http://blog.asial.co.jp/1392

コード

例えばUserが投稿した最新のメッセージの更新日時を取得したい場合に以下のような書き方が出来ます。
最後にtimestampを付けることで更新日時をタイムスタンプに変換出来ます。

$latestMessageUpdate = $user->messages()->latest()->first()->updated_at->timestamp;//1506594334

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
2