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

Cấu hình Prisma v7 Với Nest.js Mới nhất

Setup Prisma v7 trong Nest.js Bài viết dành cho ai mới học Nest.js và chọn prisma làm OR

Tấn Công Ứng Dụng Web: Mối Đe Dọa Hàng Đầu – Phần 2

viết lại nội dung này ” Phát hiện các cuộc tấn công Cross Site Scripting (XSS)

AI Chatbot 2025: Xu Hướng Tất Yếu Cho Doanh Nghiệp Dẫn Đầu

Giới thiệu AI chatbots đã trải qua một hành trình đáng kể, từ những công cụ t

Tấn Công Ứng Dụng Web: Mối Đe Dọa Hàng Đầu – Phần 1

Tấn công web là gì? Ứng dụng web là các ứng dụng cung cấp dịch vụ cho người