-
Notifications
You must be signed in to change notification settings - Fork 80
Description
Prerequisites
- Able to reproduce the behaviour outside of your code, the problem is isolated to Laravel Excel.
- Checked that your issue isn't already filed.
- Checked if no PR was submitted that fixes this problem.
Versions
- PHP version: PHP 8.2
- Laravel version: 10.41.0
- Nova version: 4.32.11
- Package version: LE: 3.1.52, LNE: 1.3.6
Description
I have a query method in a Nova action which extends Maatwebsite\LaravelNovaExcel\Actions\DownloadExcel:
public function query()
{
return $this->query
->join('locations', 'locations.id', '=', 'scans.location_id')
->leftJoin('customers', 'locations.customer_id', '=', 'customers.id')
->join('users', 'users.id', '=', 'scans.user_id')
->leftJoin('product_scan', 'scans.id', '=', 'product_scan.scan_id')
->leftJoin('products', 'products.id', '=', 'product_scan.product_id')
->leftJoin('skus', 'products.sku_id', '=', 'skus.id')
->leftJoin('borrowings', 'borrowings.scan_id', '=', 'scans.id')
->select(
[
'scans.uuid',
'scans.status',
'scans.created_at',
'scans.external_id',
'users.email as user_email',
'customers.name as customer_name',
'locations.name as location_name',
'locations.uuid as location_uuid',
'skus.brand as sku_brand',
'skus.name as sku_name',
'skus.type as sku_type',
'products.serial as product_serial',
'borrowings.status as borrowing_status',
'borrowings.sku_type as borrowing_sku_type',
]
);
}When I run this query standalone (e.g. in Tinkerwell), it works correctly. When I run it from within Nova, it generates an error:
Syntax error or access violation: 1066 Not unique table/alias: 'locations' (
Connection: mysql, SQL: select `scans`.`uuid`, `scans`.`status`, `scans`.`created_at`, `scans`.`external_id`, `users`.`email` as
`user_email`, `customers`.`name` as `customer_name`, `locations`.`name` as `location_name`, `locations`.`uuid` as `location_uui
d`, `skus`.`brand` as `sku_brand`, `skus`.`name` as `sku_name`, `skus`.`type` as `sku_type`, `products`.`serial` as `product_ser
ial`, `borrowings`.`status` as `borrowing_status`, `borrowings`.`sku_type` as `borrowing_sku_type` from `scans` inner join `loca
tions` on `locations`.`id` = `scans`.`location_id` left join `customers` on `locations`.`customer_id` = `customers`.`id` inner j
oin `users` on `users`.`id` = `scans`.`user_id` left join `product_scan` on `scans`.`id` = `product_scan`.`scan_id` left join `p
roducts` on `products`.`id` = `product_scan`.`product_id` left join `skus` on `products`.`sku_id` = `skus`.`id` left join `borro
wings` on `borrowings`.`scan_id` = `scans`.`id` inner join `locations` on `locations`.`id` = `scans`.`location_id` left join `cu
stomers` on `locations`.`customer_id` = `customers`.`id` inner join `users` on `users`.`id` = `scans`.`user_id` left join `produ
ct_scan` on `scans`.`id` = `product_scan`.`scan_id` left join `products` on `products`.`id` = `product_scan`.`product_id` left j
oin `skus` on `products`.`sku_id` = `skus`.`id` left join `borrowings` on `borrowings`.`scan_id` = `scans`.`id` where `scans`.`i
d` in...
If I extract the query and just show the joins, it looks like this:
from `scans`
inner join `locations` on `locations`.`id` = `scans`.`location_id`
left join `customers` on `locations`.`customer_id` = `customers`.`id`
inner join `users` on `users`.`id` = `scans`.`user_id`
left join `product_scan` on `scans`.`id` = `product_scan`.`scan_id`
left join `products` on `products`.`id` = `product_scan`.`product_id`
left join `skus` on `products`.`sku_id` = `skus`.`id`
left join `borrowings` on `borrowings`.`scan_id` = `scans`.`id`
inner join `locations` on `locations`.`id` = `scans`.`location_id`
left join `customers` on `locations`.`customer_id` = `customers`.`id`
inner join `users` on `users`.`id` = `scans`.`user_id`
left join `product_scan` on `scans`.`id` = `product_scan`.`scan_id`
left join `products` on `products`.`id` = `product_scan`.`product_id`
left join `skus` on `products`.`sku_id` = `skus`.`id`
left join `borrowings` on `borrowings`.`scan_id` = `scans`.`id`While you can see there are no duplicate joins in my query builder code, there are duplicate joins in the generated query, and that part isn't done by my code. If I replace any table name with an alias, for example:
->join('locations as locations2', 'locations2.id', '=', 'scans.location_id')I get the same duplicate table error on the new name, even though I'm clearly only specifying it once. To confirm that these query clauses are generated from this code and not elsewhere, if I remove one join from this builder, both that join and its duplicate disappear from the resulting query.
Essentially I can't do any query that contains a join.
There is a very similar bug reported in Nova itself, but it was closed without being resolved; I don't know where the problem actually lies, whether in Nova or LNE.
I expect this code to generate a query that doesn't contain duplicate tables. I don't see how I could cause this to happen intentionally, so I can only assume it's a bug.