Skip to content

Result of a method call on Eloquent\Builder depends on the method casing used #48825

Closed
@luka-papez

Description

@luka-papez

Laravel Version

10.29.0

PHP Version

8.1.2

Database Driver & Version

Not relevant but just in case: psql (PostgreSQL) 14.9 (Ubuntu 14.9-0ubuntu0.22.04.1)

Description

When calling a method on a instance of Eloquent\Builder, the result of the method will depend on the method casing used. I discovered this when I called $query->toSQL() instead of $query->toSql() and got back a Builder instead of a string which I expected. Given that PHP is case-insensitive about method names, I find this behavior surprising.

According to my understanding, the issue happens on this line:

The Builder decides what to do with the result depending on whether the called method is found in the $passthru property. Since toSql is there, and toSQL is not, the result is different.

I believe the behavior could be fixed by lowercasing the method name and making the $passthru property contain lowercased method names, but I am not sure what kind of implications that might have down the road.

If needed, I would be happy to volunteer some time into fixing this issue, or reviewing the fix. Either way, thank you for all the hard work on making Laravel so great for the rest of us!

Steps To Reproduce

The behavior can be triggered by the following snippet:

        $query = User::query()->select('*');

        $camelCaseResult = $query->toSql();  // this returns a string
        $upperCaseResult = $query->toSQL();  // this returns $query

A small repository demonstrating this behavior can be found here:

It's based on the example Laravel app with a separate commit demonstrating the potential problem

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions