0

I have a query and I tried to convert it to laravel query builder style. However I couldn't succeed.

This is my query

SELECT
  count(*)
    FILTER (WHERE order_status_id IN (5, 4, 15))        AS ready_for_delivery,
  count(*)
    FILTER (WHERE order_status_id IN (6))               AS out_for_delivery,
  count(*)
    FILTER (WHERE order_status_id IN (4, 10, 9, 12, 7)) AS completed_order,
  count(*)
    FILTER (WHERE order_status_id IN (11, 16, 17, 13))  AS waiting_for_cancellation_return_change
FROM order_item_histories;

This is what I got from http://www.midnightcowboycoder.com/ which obviously doesn't work. But it is a starting point

DB::table('order_item_histories')
        ->selectSub('count', 'ready_for_delivery')
        ->selectSub('FILTER', 'ready_for_delivery')
        ->selectSub('count', 'out_for_delivery')
        ->selectSub('FILTER', 'out_for_delivery')
        ->selectSub('count', 'completed_order')
        ->selectSub('FILTER', 'completed_order')
        ->selectSub('count', 'waiting_for_cancellation_return_change')
        ->selectSub('FILTER', 'waiting_for_cancellation_return_change')
        ->get();

This query works but it is just raw. I know no harm but..

        $aa = DB::table('order_item_histories')
            ->select(DB::raw('count(*)
    FILTER (WHERE order_status_id IN (5, 4, 15))        AS ready_for_delivery,
  count(*)
    FILTER (WHERE order_status_id IN (6))               AS out_for_delivery,
  count(*)
    FILTER (WHERE order_status_id IN (4, 10, 9, 12, 7)) AS completed_order,
  count(*)
    FILTER (WHERE order_status_id IN (11, 16, 17, 13))  AS waiting_for_cancellation_return_change'))
            ->get();
3
  • 1
    if it ain't broke, don't fix it Commented Apr 3, 2018 at 19:42
  • @Ohgodwhy That's one approach :) Commented Apr 3, 2018 at 19:46
  • I think multiple calls like this will work ->selectSub('count(*) FILTER (WHERE order_status_id IN (5, 4, 15))', 'ready_for_delivery') but it's perfectly fine to write raw queries is this case, just beware of SQL injection Commented Apr 3, 2018 at 21:08

2 Answers 2

1
DB::table('order_item_histories')
    ->selectRaw('count(*) FILTER (WHERE order_status_id IN (?, ?, ?)) AS ready_for_delivery', [5, 4, 15])
    ->selectRaw('count(*) FILTER (WHERE order_status_id IN (?)) AS out_for_delivery', [6])
    ->selectRaw('count(*) FILTER (WHERE order_status_id IN (?, ?, ?, ?, ?)) AS completed_order', [4, 10, 9, 12, 7])
    ->selectRaw('count(*) FILTER (WHERE order_status_id IN (?, ?, ?, ?))  AS waiting_for_cancellation_return_change', [11, 16, 17, 13])
    ->get();
Sign up to request clarification or add additional context in comments.

Comments

1

Try using case instead of filter

$users = DB::table('order_item_histories')
                 ->select(DB::raw('SUM(CASE WHEN order_status_id IN (5, 4, 15) THEN 1 ELSE 0 END) AS ready_for_delivery'))
                 ->select(DB::raw('SUM(CASE WHEN order_status_id IN (6) THEN 1 ELSE 0 END) AS out_for_delivery'))
                 ->select(DB::raw('SUM(CASE WHEN order_status_id IN (4, 10, 9, 12, 7) THEN 1 ELSE 0 END) AS completed_order'))
                 ->select(DB::raw('SUM(CASE WHEN order_status_id IN (11, 16, 17, 13) THEN 1 ELSE 0 END) AS waiting_for_cancellation_return_change'))
                 ->get();

2 Comments

Didn't give me expected output.
What result did you got

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.