如何使用 Laravel 查询生成器获取最后更新的记录?
How to Get Last Updated Record with Laravel Query Builder?
我正在使用 Laravel 查询生成器从数据库中获取最后更新的记录。但是,它显示不同的 id
和不同的 update_date.
update_date
是正确的,它显示最后一个 updated_date,但每次我得到第一个 id.
$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
和 table 中的第一个 ID。
试试这个:
orderBy('updated_at','DESC')->first()->id
这里是获取最新更新记录的id。当您删除 first() 方法时,您将获得按 'updated_at'
排序的整个集合
我正在使用 Laravel 查询生成器从数据库中获取最后更新的记录。但是,它显示不同的 id
和不同的 update_date.
update_date
是正确的,它显示最后一个 updated_date,但每次我得到第一个 id.
$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
和 table 中的第一个 ID。
试试这个:
orderBy('updated_at','DESC')->first()->id
这里是获取最新更新记录的id。当您删除 first() 方法时,您将获得按 'updated_at'
排序的整个集合