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

Eloquence treats parameter grouping query as subquery #261

Open
carlfredrikhero opened this issue Oct 9, 2020 · 3 comments
Open

Eloquence treats parameter grouping query as subquery #261

carlfredrikhero opened this issue Oct 9, 2020 · 3 comments

Comments

@carlfredrikhero
Copy link

Laravel support parameter grouping via a where method that takes exaktly 1 argument (a closure) : https://laravel.com/docs/7.x/queries#parameter-grouping

$users = DB::table('users')
           ->where('name', '=', 'John')
           ->where(function ($query) {
               $query->where('votes', '>', 100)
                     ->orWhere('title', '=', 'Admin');
           })
           ->get();

produces

select * from users where name = 'John' and (votes > 100 or title = 'Admin')

Subqueries are also supported with almost the same syntax. The only difference is that the where method takes a closure as the first argument and a value as a second argument.

$users = User::where(function ($query) {
    $query->select('type')
        ->from('membership')
        ->whereColumn('user_id', 'users.id')
        ->orderByDesc('start_date')
        ->limit(1);
}, 'Pro')->get();

Notice the "'Pro'" value on the last line.

Eloquence seems to interpret where(closure) as where(closure,null).

Im my case Eloquence changes the correct query:

select * from users where (name LIKE ?)

to the syntactically incorrect query

select * from users where (select * where name LIKE ?) is null

The php code to generate the query is:

$sql = static
            ::where(
                function ($query) use ($q) {
                    $query
                        ->where('name', 'LIKE', '%'.$q.'%');
                }
            )
            ->toSql();

I will try to troubleshoot this further.

@EK1771
Copy link

EK1771 commented Dec 7, 2020

Check out jarektkaczyk/hookable#27, still waiting for it to be merged but it worked for me.

@PeraMika
Copy link

PeraMika commented Dec 8, 2020

@jarektkaczyk :)

@gfernandez-me
Copy link

Oh, I'm facing the same problem.

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

4 participants