Skip to content

Eloquence treats parameter grouping query as subquery #261

Open
@carlfredrikhero

Description

@carlfredrikhero

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions