It's either a Query Builder or MySQL bug. I just ran into it. Union wraps everything in parenthesis, and MySql doesn't like it.
This is what the Builder produces:
( SELECT... ) UNION ( ( SELECT... ) UNION ( SELECT... ) )
This is what is should produce:
( SELECT... ) UNION ( SELECT... ) UNION ( SELECT... )
I fixed it by modifying \Illuminate\Database\Query\Grammers\MysqlGrammar::compileUnion
protected function compileUnion(array $union)
{
$joiner = $union['all'] ? ' union all ' : ' union ';
$sql = $union['query']->toSql();
return $joiner.(preg_match( '/^\(.*\)$/', $sql ) ? $sql : "($sql)" );
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community