LoginSignup
1
0

More than 1 year has passed since last update.

laravel Eloquentモデルを用いて最も大きいIDのレコードを取得する

Last updated at Posted at 2022-11-29

概要

  • 最も大きいIDのレコードを取得する方法をメモ的にまとめる。

方法

  • orderByでidの降順にして最初のものを取得する。

    $user = new User();
    $user->orderBy('id', 'desc')->first();
    
  • latest()ordest()メソッド利用する。(ただこれは「最も新しいID」のレコードを取得するというよりは、特に設定しなかった場合created_atの値でソートしている。逆に「レコード作成日時が最も新しいレコード」を取得したい場合はこちらを使ったほうが意味的に合っている。)

    $user = new User();
    $user->latest()->first();
    

参考文献

1
0
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
1
0