After upgrading to Laravel 10 my eloquent query using joinSub
fromSub
returns an error
error evaluating code: str_starts_with(): Argument #1 ($haystack) must be of type string, IlluminateDatabaseQueryExpression given
My query looks something like that (simplified version)
DB::connection('connection1')
->table('table1')
->joinSub(DB::connection('connection2')->query()
->fromSub(function (Builder $builder) {
$builder
->from('table2')
->select(['emailId', 'link'])
->selectRaw('COUNT(link) as total')
->groupBy(['emailId', 'link'])
->orderByDesc('total');
}, 'clicked')
->select('clicked.emailId')
->groupBy(['clicked.emailId'])
, 'clicked2', 'clicked2.emailId', '=', 'email.id'
)
->select('table1.*')
->selectRaw('GROUP_CONCAT(clicked2.totals) as clicked_links');
It used to work in Laravel <10 but now when a method prependDatabaseNameIfCrossDatabaseQuery()
in IlluminateDatabaseQueryBuilder
gets executed (see code below) it breaks at ! str_starts_with($query->from, $databaseName)
because $query->from
is an Expression
and __toString()
has been removed from it in Laravel 10
protected function prependDatabaseNameIfCrossDatabaseQuery($query)
{
if ($query->getConnection()->getDatabaseName() !==
$this->getConnection()->getDatabaseName()) {
$databaseName = $query->getConnection()->getDatabaseName();
if (! str_starts_with($query->from, $databaseName) && ! str_contains($query->from, '.')) {
$query->from($databaseName.'.'.$query->from);
}
}
return $query;
}
Any ideas how to fix this? As I can’t seemed to apply $expression->getValue(DB::connection()->getQueryGrammar());
mentioned in the documentation as I don’t use DB::raw(
and don’t cast it to string in my code.