Laravel查询以检索上次报告ID

时间:2017-08-23 09:26:33

标签: laravel laravel-5.2

使用此查询,我收到第一个报告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是我最新的报告ID。

5 个答案:

答案 0 :(得分:0)

检查一下:

 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();

答案 1 :(得分:0)

带有Linux 3.10.0-514.el7.x86_64 x86_64

Order by service_report_mapping.id,第一个结果将是最后一个ID:

DESC

答案 2 :(得分:0)

你应该试试这个:

只需将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();

答案 3 :(得分:0)

给出的信息不多......
你试过吗

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();

你的小组背后的最初想法是什么?顺便说一下?似乎没必要吗?

答案 4 :(得分:0)

我得到了解决方案。使用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();