Sorting Eloquent Union query results
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Sorting Eloquent Union Query Results: Understanding the Challenges and Solutions
Body:
In Eloquent ORM, when working with database queries that involve union operations, sometimes it can be challenging to apply sorting properly on multiple models before unifying their results. Let's break down why this happens and explore possible solutions to achieve our desired output.
1. Understanding the Issue:
In the provided code example, you have two separate Eloquent model queries ($a and $b) that are being unioned together in order to obtain a combined result set. The issue arises when applying 'orderBy()' on each query individually and then trying to union them.
2. Affect of Ordering:
The reason for this issue is the way Eloquent ORM handles queries - it first processes the individual queries in the sequence they are defined, retrieves their results, and then merges them together based on their fields' values. In this case, since the ordering happens after unionization, there will not be any influence on the sorting of each individual query before merging them.
3. Alternative Approaches:
1. Use MySQL-style syntax directly in your queries:
If you are okay with using raw SQL queries within your Eloquent models, you can achieve the same results by running union queries as follows:
$a = Model::raw("(SELECT * FROM (SELECT * FROM $table1 WHERE `field` = ? AND `condition_a` = 1 ORDER BY layout) UNION ALL (SELECT * FROM $table2 WHERE `field` = ? AND `condition_b` = 1 ORDER BY layout))", [$code, $code]);
$unionResult = Model::raw("(SELECT * FROM (SELECT * FROM $a WHERE 1=1 ORDER BY FIELD(`layout`, 'normal', 'split', 'flip', 'double-faced') ASC) UNION ALL (SELECT * FROM $b WHERE 1=1 ORDER BY FIELD(`layout`, 'normal', 'split', 'flip', 'double-faced') ASC))");
In this approach, the ordering is applied directly in the raw SQL queries for each table before unionizing their results. Keep in mind that this solution will require you to have a well-structured database schema.
2. Use different strategies:
If your aim is to sort the models based on a specific field and then unionize them, you can separate the ordering process from the unification by performing the following steps:
1. Union both queries separately without any ordering.
2. Merge the results of those queries using another Eloquent model or a simple array_merge function in PHP.
3. Apply your desired sorting on the combined result set.
This strategy will ensure that you achieve the correct sorting order for each individual query, followed by the unionized result set.
Conclusion:
Sorting Eloquent union query results can be challenging due to its internal processing sequence. However, by understanding how it works and adopting alternative approaches mentioned above, you can overcome this issue and achieve your desired output. Always remember that sometimes, using a mix of Eloquent ORM and raw SQL queries might be a better solution than trying to bend the rules for each query.