首页 > 解决方案 > 如何使用 Laravel Query Builder 获取最后更新的记录?

问题描述

我正在使用 Laravel Query Builder 从数据库中获取最后更新的记录。但是,它显示不同的id不同是正确的,它显示了我上次更新的日期,但每次我得到第一个update_date.update_dateid.

$results = DB::table('customer_info as cust_info')
    ->join('client_emp as client_info', 'cust_info.id', '=', 'client_info.cust_id')
    ->select('cust_info.id', DB::raw('MAX(cust_info.updated_at)'))
    ->orderBy('cust_info.id','DESC')
    ->first();

在上面的查询中,我编写了 select 来获取id最后更新的记录。

->select('cust_info.id', DB::raw('MAX(cust_info.updated_at)'))

但是,它updated_date每次只向我显示最后一个表中的第一个 id。

标签: laravellaravel-query-builder

解决方案


尝试这个:

orderBy('updated_at','DESC')->first()->id

在这里,您将获得最新更新记录的 id。当您删除 first() 方法时,您将获得按“updated_at”排序的整个集合


推荐阅读