0

The code below is not working.

$subQuery = DB::table('table1')->groupBy('col');

$data = DB::table($subQuery, 'sub')->get();

Can you help me with this?

1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Mar 4, 2022 at 15:25

1 Answer 1

5

Your code is already functional. The only alternative (to make the same query) is to inline the $subQuery part.

$subQuery = DB::table('table1')->groupBy('col');

$data = DB::table($subQuery, 'sub')->get();

Is the same as

$data = DB::table(function ($sub) {
        $sub->from('table1')
            ->groupBy('col');
    }, 'sub')
    ->get();

or

$data = DB::table(DB::table('table1')->groupBy('col'), 'sub')->get();
Sign up to request clarification or add additional context in comments.

Comments

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.