Skip to content

value method on Query Builder returns _id instead of passed value #1741

Closed
@goodevilgenius

Description

@goodevilgenius

Take the following code:

app('db')->connection('mongodb')->table($collectionName)
    ->where($key, $value)->value($propertyName);

If using mysql, it runs a query like:

SELECT $propertyName FROM $collectionName WHERE $key = $value LIMIT 1

And it returns the value of $propertyName.

See https://github.com/illuminate/database/blob/8b600637ff7f472bc41705b5a7dabe9501eebe56/Query/Builder.php#L2027

However, due to the fact that mongo always returns the _id, whenever you use a projection, this method always returns _id, instead of $propertyName

This method should be overriden with something like:

public function value($column)
{
    $result = (array) $this->first([$column]);

    return $result[$column] ?? null;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions