Skip to content
Advertisement

Laravel DB::select is returning only the last 2 columns listed in the SELECT statement

I’ve created a query that runs perfectly on phpMyAdmin:

query working perfectly on phpMyAdmin

However when I run this by using DB::select() it only displays the last 2 columns in the SELECT statement.

Expected result: The query should output the same result when run on the phpMyAdmin prompt and the Laravel DB::select() function.

Actual result: the query result is wrong when run with the DB::select() function. example run with PHP artisan tinker:

JavaScript

outputs:

JavaScript

As you can see, only the last 2 columns of the select are ending up in the output ;(

The very weird thing is that if I concatenate the columns in the select statement, then it displays them , so they are actually being selected, but they aren’t ending up in the output for some reason.

For example if I run

JavaScript

then I get this result:

JavaScript

Which has all of the column values! Why are the column values not ending up in the output unless I use concat?

Advertisement

Answer

That’s because you are fetching the results into an associative array. You have 3 columns with the exact same name, meaning they get overwritten. Change your query to use aliases: SELECT tipi_coinvolgimento.nome as co_nome, users.nome as u_nome, ...

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement