Cách lấy last record of database table trong Laravel

Trong dự án nhiều khi chúng ta muốn lấy bản ghi cuối cùng trong 1 table nào đó. Để giải quyết vấn đề này chúng ta có thể sử dụng hàm latest() hoặc orderBy() trong Laravel 6+. Mình sẽ demo 3 cách lấy bản ghi cuối cùng trong table ở các ví dụ bên dưới

Trong dự án nhiều khi chúng ta muốn lấy bản ghi cuối cùng trong 1 table nào đó.
Để giải quyết vấn đề này chúng ta có thể sử dụng hàm latest() hoặc orderBy() trong Laravel 6+.
Mình sẽ demo 3 cách lấy bản ghi cuối cùng trong table ở các ví dụ bên dưới nhé.
Đầu tiên mình sẽ tạo một table items có các dữ liệu demo như bên dưới.

Using latest() belong to created_at field:

Khi sử dụng latest() mặc định nó sẽ lấy bản ghi cuối cùng dựa vào trường created_at trong table items.
Thêm code vào controller như bên dưới.
appHttpControllersItemController.php

<?phpnamespaceAppHttpControllers;useAppModelsItem;classItemControllerextendsController{/**
     * Display a listing of the resource.
     *
     * @return IlluminateHttpResponse
     */publicfunctionindex(){$last_record=Item::latest()->first()->toArray();dd($last_record);}}

Output:

array:7 [▼
  "id" => 4
  "title" => "Four"
  "description" => "[email protected]"
  "is_active" => 1
  "deleted_at" => null
  "created_at" => "2021-12-30T11:16:47.000000Z"
  "updated_at" => null
]

Using orderBy() belong to id field:

appHttpControllersItemController.php

<?phpnamespaceAppHttpControllers;useAppModelsItem;classItemControllerextendsController{/**
     * Display a listing of the resource.
     *
     * @return IlluminateHttpResponse
     */publicfunctionindex(){$last_record=Item::orderBy('id','DESC')->first()->toArray();dd($last_record);}}

Output:

array:7 [▼
  "id" => 4
  "title" => "Four"
  "description" => "[email protected]"
  "is_active" => 1
  "deleted_at" => null
  "created_at" => "2021-12-30T11:16:47.000000Z"
  "updated_at" => null
]

Using latest() belong to id field:

appHttpControllersItemController.php

<?phpnamespaceAppHttpControllers;useAppModelsItem;classItemControllerextendsController{/**
     * Display a listing of the resource.
     *
     * @return IlluminateHttpResponse
     */publicfunctionindex(){$last_record=Item::latest('id')->first()->toArray();dd($last_record);}}

Output:

array:7 [▼
  "id" => 4
  "title" => "Four"
  "description" => "[email protected]"
  "is_active" => 1
  "deleted_at" => null
  "created_at" => "2021-12-30T11:16:47.000000Z"
  "updated_at" => null
]

Hy vọng bài viết này sẽ giúp ích cho các bạn!

Nguồn: viblo.asia

Bài viết liên quan

7 Cách Tăng Tốc Ứng Dụng React Hiệu Quả Mà Bạn Có Thể Làm Ngay

React là một thư viện JavaScript phổ biến trong việc xây dựng giao diện người d

Trung Quốc “thả quân bài tẩy”: hàng loạt robot hình người!

MỘT CUỘC CÁCH MẠNG ROBOT ĐANG HÌNH THÀNH Ở TRUNG QUỐC Thượng Hải, ngày 13/5 –

9 Mẹo lập trình Web “ẩn mình” giúp tiết kiệm hàng giờ đồng hồ

Hầu hết các lập trình viên (kể cả những người giỏi) đều tốn thời gian x

Can GPT-4o Generate Images? All You Need to Know about GPT-4o-image

OpenAI‘s GPT-4o, introduced on March 25, 2025, has revolutionized the way we create visual con