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

[10.x] Fix an error when $query->from is of type Illuminate\Database\Query\Expression #49937

Closed
wants to merge 1 commit into from

Conversation

kztomita
Copy link

@kztomita kztomita commented Feb 1, 2024

When passing a subquery referencing a different database to joinSub(), the prependDatabaseNameIfCrossDatabaseQuery() method would rewrite the "from" clause of the subquery. However, if $query->from is not a string but an Illuminate\Database\Query\Expression, the following error occurs:

TypeError: str_starts_with(): Argument #1 ($haystack) must be of type string, Illuminate\Database\Query\Expression given

To address this issue, I made modifications to prevent the replacement process when $query->from is an Illuminate\Database\Query\Expression instead of a string.

@driesvints driesvints changed the title [10.x] Fix an error when $query->from is of type Illuminate\Database\Query\Expression. [10.x] Fix an error when $query->from is of type Illuminate\Database\Query\Expression Feb 1, 2024
@taylorotwell
Copy link
Member

Don't really have the bandwidth to chase this down at the moment. Don't pass an expression for now.

@MarFelix
Copy link

Hello, any other way to correct this error ?

@kztomita
Copy link
Author

@MarFelix
To avoid this issue, I think the only way is to pass the subquery's FROM clause as a string rather than as an Expression.

Example:

    /*
    $subquery = $builder2
                  ->from(new Raw('database2.posts'))  // Expression
                  ->select('user_id, title');
    */
    $subquery = $builder2
                  ->from('posts')  // string
                  ->select('user_id, title');

    $builder = $this->getBuilder();
    $builder->from('users')->joinSub($subquery, 'sub', function ($join) {
        $join->on('users.id', '=', 'sub.user_id');
    });

Refer to the unit tests in https://github.com/laravel/framework/pull/49937/files.

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

Successfully merging this pull request may close these issues.

3 participants