Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nalexander's avatar

Laravel 7.2.5 finds duplicate aliases in mixed DB Query

maintaining some code that has DB queries that mix DB::raw with dbQuery. When a field is selected as an alias in a DB::raw query and then combined with dbquery left joins in Laravel 7.25.0 - it may complain that it has encountered a duplicate value for one or more of the aliases.

It appears to work without error in Laravel 7.2.0

0 likes
2 replies
jlrdw's avatar

You have to use your table name also.

table.field

If two tables in a join have an ID column there will be confusion unless you use table names.

ID is just example that would be true for any column.

nalexander's avatar

It did not occur to me that the query was that flakey - it looked like a version bug as it suddenly broke after a composer update. But carefully identifying the table in each clause runs the query. Of course. It was the reported dups on the aliases that threw me - but it now makes perfect sense. Duh?!

Sorry, I can not post code as it belongs to another company and we are going to start again as this was a relatively minor problem compared to others (database is not normalised, the controllers are 15k lines long with massive amounts of raw SQL....) - but wanted to keep the existing version running for while while we do it properly.

Please or to participate in this conversation.