Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Support Union #30

Open
kassah opened this issue Oct 23, 2023 · 0 comments
Open

Feature: Support Union #30

kassah opened this issue Oct 23, 2023 · 0 comments

Comments

@kassah
Copy link

kassah commented Oct 23, 2023

This request is to add a feature for SQL Unions.

Laravel Docs: Laravel Eloquent Unions
MySQL Docs: MySQL 8.1 UNION Clause

Union Example

Example SQL input:

(select * from `users` where `last_name` is null) union (select * from `users` where `first_name` is null)

Example Eloquent output:

DB::table('users')
->whereNull('last_name')
->union(
            DB::table('users')
            ->whereNull('first_name')
)
->get();

Union All Example

Example SQL input:

(select * from `users` where `last_name` is null) union all (select * from `users` where `first_name` is null)

Example Eloquent output:

DB::table('users')
->whereNull('last_name')
->unionAll(
            DB::table('users')
            ->whereNull('first_name')
)
->get();

Union with orderBy on id

Example SQL input:

(select * from `users` where `last_name` is null) 
union
(select * from `users` where `first_name` is null) 
order by `id` asc

Example Eloquent output:

DB::table('users')
->whereNull('last_name')
->unionAll(
            DB::table('users')
            ->whereNull('first_name')
)
->orderBy('id')
->get();

Additional Context

While Eloquent doesn't handle returning a UNION from multiple models well (everything will be shoved into the first model's class), it does handle it well as a subselect with multiple models. It makes it much easier to scan across many different models as part of a very complex where.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant