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

johnw65's avatar

Oracle Return Column Names of Raw Query

Currently I have a code which grab


$data = DB::table('queryTable')
    	->select( 'querysql')
    	->where('queryTable_key', '=' , $id)   
    	->first();

$sqlQuery = $data->querysql;

sample $sqlQuery="SELECT
 CD.FISCAL_YEAR,
 CD.PROG,
 FCI.DT_EXPD
FROM
 COMDATA CD,
 FASTCI FCI
WHERE
CD.FISCAL_YEAR = FCI.FISCAL_YEAR 

ORDER BY
 CD.FISCAL_YEAR,
 CD.ID;

How can I grab all of the column names for the query above which can be multiple joins.

0 likes
4 replies
tykus's avatar
tykus
Best Answer
Level 104

The column names should be the keys in the stdClass object that is returned (if there is one).

Otherwise, if you have the query builder instance, you can interrogate the columns property, e.g.

$builder = DB::table('queryTable') ->select( 'querysql') ->where('queryTable_key', '=' , $id); // not completed with `first()`
$columns = $builder->columns;
$data = $builder->first();
Tray2's avatar

Not sure what you mean, or is trying to do.

Since it seems to be related to your tax law, I would be extremely careful when writing the code.

Can you please explain a bit better what you data looks like and what the expected result is?

I just love the old join syntax and the optional parameter

CD.FISCAL_YEAR = FCI.FISCAL_YEAR (+)

Just be aware the Laravel doesn't support the oracle SQL to 100%. so I suggest working with database views instead of writing the queries in the query builder.

johnw65's avatar

@Tray2

Tray2, I have a table queryTable, and my DB Admin have written a sql statement in queryTable->querysql which should run a query and export the query into either MS Excel Spreadsheet format or PDF format. So I initially need to grab all of the columns that are in queryTable->querysql so I can display them when exporting into either Excel format or PDF format. Trying to export to Excel without using any plugins.

In regards to the data, depending on the query, it varies. But need to output into Excel or PDF. Thanks.

Please or to participate in this conversation.