Skip to content

Like operator doesn't work with number #2138

Closed
@Kyshino

Description

@Kyshino
  • Laravel-mongodb Version: 6.3.1
  • PHP Version: 7.4

Description:

When I try to use the LIKE operator with a number, it doesnt work,

Steps to reproduce

  1. Use $query->where("column", "LIKE", "%29%");

Expected behaviour

Return all rows that contain the value 29 in column

Actual behaviour

Returns nothing

Possible solution

The problem is in the file mongodb/Query/Builder.php specifically in the function called compileWhereBasic.
In this code

       if (in_array($operator, ['like', 'not like'])) {
            if ($operator === 'not like') {
                $operator = 'not';
            } else {
                $operator = '=';
            }

$operator change from like to = and then in this other code, a few lines down

        if (!isset($operator) || $operator == '=') {
            if ($is_numeric) {
                $query = ['$where' => '/^'.$value->getPattern().'/.test(this.'.$column.')'];
            } else {
                $query = [$column => $value];
            }

That check if operator is '=' (what changed a few lanes before) and $is_numeric is true (this is true because our like is with 29) change the query to that and then it doesnt return nothing.

I tried to replace

            if ($is_numeric) {
                $query = ['$where' => '/^'.$value->getPattern().'/.test(this.'.$column.')'];
            } else {
                $query = [$column => $value];
            }

for

$query = [$column => $value];

and its working.

Logs: SyntaxError: identifier starts immediately after numeric literal (1/1) CommandException SyntaxError: identifier starts immediately after numeric literal

in Count.php line 176

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions