Closed
Description
- Laravel Version: 7.10 to 8.x
- PHP Version: 7.3
- Database Driver & Version: mysql
Description:
Can not paginate a query build with QueryBuilder when it has a join and we do select all fields with *.
Steps To Reproduce:
When using query builder to generate a query with a join if we select all fields with *, use a groupBy and paginate the query it crash with a Duplicate column name 'id':
Illuminate\Database\QueryException
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'id' (SQL: select count(*) as aggregate from (SELECT * FROM `order_contents`
INNER JOIN `orders` ON `GS_orders`.`id` = `order_contents`.`order_id`
INNER JOIN `menu_items` ON `menu_items`.`id` = `order_contents`.`item_id`) as `aggregate_table`)
I've realised that this was added on laravel 7.10 version:
class QueryBuilder {
...
protected function runPaginationCountQuery($columns = ['*']) {
if ($this->groups || $this->havings) {
$clone = $this->cloneForPaginationCount();
if (is_null($clone->columns) && ! empty($this->joins)) {
$clone->select($this->from.'.*');
}
return $this->newQuery()
->from(new Expression('('.$clone->toSql().') as '.$this->grammar->wrap('aggregate_table')))
->mergeBindings($clone)
->setAggregate('count', $this->withoutSelectAliases($columns))
->get()->all();
}
...
}
I think that, if we are grouping, there is no need to keep selected columns when paginating, so we could always select $this->from.*
when there is a join, not only when there a join and no columns are selected...
Metadata
Metadata
Assignees
Labels
No labels