【问题标题】:How to Get Last Updated Record with Laravel Query Builder?如何使用 Laravel Query Builder 获取最后更新的记录?
【发布时间】:2019-01-26 14:21:29
【问题描述】:

我正在使用 Laravel Query Builder 从数据库中获取最后更新的记录。但是,它显示不同的id 和不同的update_date. update_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 以及表中的第一个 id。

【问题讨论】:

    标签: laravel laravel-query-builder


    【解决方案1】:

    试试这个:

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

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

    【讨论】:

      猜你喜欢
      • 2018-12-18
      • 1970-01-01
      • 1970-01-01
      • 2015-07-16
      • 1970-01-01
      • 2015-10-02
      • 2013-12-17
      • 2014-03-05
      • 1970-01-01
      相关资源
      最近更新 更多