Laravel 查询以检索最后一个报告 ID

Laravel Query to retrieve last report id

使用此查询,我获得了第一个报告 ID,但我想检索最新的服务报告 ID。

提前致谢。

 CustomerRequest::where('escalated', '=', 0)
    ->whereIn('customer_request.complain_status_id', array(9, 10))
    ->leftjoin('service_report_mapping', 'service_report_mapping.complain_id', '=', 'customer_request.id')
    ->select('service_report_mapping.report_id')
    ->orderBy('service_report_mapping.report_date', 'DESC')
    ->groupBy('customer_request.id')
    ->get();

输出: service_report_id : 20903

服务报告 ID:20903 47185 87609 98661 98662

98662 是我最新的报告编号。

检查这个:

 CustomerRequest::where('escalated', '=', 0)
    ->whereIn('customer_request.complain_status_id', array(9, 10))
    ->leftjoin('service_report_mapping', 'service_report_mapping.complain_id', '=', 'customer_request.id')
    ->select('service_report_mapping.report_id')
    ->orderBy('service_report_mapping.id', 'DESC')
    ->groupBy('customer_request.id')
    ->get();

Order by service_report_mapping.idDESC 并且第一个结果将是最后一个 ID:

 CustomerRequest::where('escalated', '=', 0)
    ->whereIn('customer_request.complain_status_id', array(9, 10))
    ->leftjoin('service_report_mapping', 'service_report_mapping.complain_id', '=', 'customer_request.id')
    ->select('service_report_mapping.report_id')
    ->orderBy('service_report_mapping.id', 'DESC')
    ->groupBy('customer_request.id')
    ->get();

你应该试试这个:

只需将 orderBy 放在 groupBy 之后并检查:

CustomerRequest::where('escalated', '=', 0)
    ->whereIn('customer_request.complain_status_id', array(9, 10))
    ->leftjoin('service_report_mapping', 'service_report_mapping.complain_id', '=', 'customer_request.id')
    ->select('service_report_mapping.report_id')
    ->groupBy('customer_request.id')
    ->orderBy('service_report_mapping.report_date', 'DESC')
    ->get();

CustomerRequest::where('escalated', '=', 0)
    ->whereIn('customer_request.complain_status_id', array(9, 10))
    ->leftjoin('service_report_mapping', 'service_report_mapping.complain_id', '=', 'customer_request.id')
    ->select('service_report_mapping.report_id')
    ->groupBy('customer_request.id')
    ->orderBy('service_report_mapping.id', 'DESC')
    ->get();

没有提供太多信息...
你试过了吗

CustomerRequest::where('escalated', '=', 0)
->whereIn('customer_request.complain_status_id', array(9, 10))
->leftjoin('service_report_mapping', 'service_report_mapping.complain_id', '=', 'customer_request.id')
->select('service_report_mapping.report_id')
->latest();

顺便说一下,您的 groupBy 最初的想法是什么?好像没必要?

我得到了解决方案。使用 max() 我正在获取上次报告 ID。

谢谢大家的回答。

 CustomerRequest::where('escalated', '=', 0)
    ->whereIn('customer_request.complain_status_id', array(9, 10))
    ->leftjoin('service_report_mapping', 'service_report_mapping.complain_id', '=', 'customer_request.id')
    ->select('max(service_report_mapping.report_id)')
    ->orderBy('service_report_mapping.report_date', 'DESC')
    ->groupBy('customer_request.id')
    ->get();