1

I want to take this SQL query to a query in Laravel Query Builder. It is important that the result be of type Query Builder. Please if you can help me.

SELECT MAX(sub.total) as total, sub.vendedor
FROM
(
    SELECT COUNT(id) as total, vendedor  
    FROM data_reports dr
    GROUP BY vendedor
) sub

I have the following but I couldn't go any further.

$data = DB::table('data_reports')
    ->select(DB::raw('COUNT(id) as total, vendedor'))
    ->groupBy('vendedor');

1 Answer 1

1

Here is the query:

$results = DataReport::select(DB::raw('MAX(sub.total) as total, sub.vendedor'))
->from(function ($query) {
    $query->select(DB::raw('COUNT(id) as total, vendedor'))
        ->from('data_reports')
        ->groupBy('vendedor');
}, 'sub')
->get();
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much arya_la. I modified it so that the result is of type 'Illuminate\\Database\\Query\\Builder' and not 'Illuminate\\Database\\Eloquent\\Builder'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.