I think you might be overthinking a little here.
What is it that you want to do that makes you think you need to add a shitloads of union alls to your query?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I need to construct a query of the following structure:
SELECT
...
FROM table
WHERE
...
GROUP BY
...
UNION ALL
SELECT
...
FROM table
WHERE
...
GROUP BY
...
UNION ALL
SELECT
...
FROM table
WHERE
...
GROUP BY
...
.. and so on..
I get some data in an array, then I iterate it in a foreach loop where I construct each SELECT individually.
But I am not able to find a way to add that UNION ALL after every SELECT (Except for the last one):
$query = DB::table('table');
foreach ($data as $item) {
$query->.. // construct the SELECT
$query->unionAll($query); // does not work
}
How can I do it?
Thanks!
@Ligonsker laravel has the native instance, use db facade or getPdo().
See https://laracasts.com/discuss/channels/laravel/sql-native-to-query-builder
Bind parameters.
Please or to participate in this conversation.