How can I get the column field names from a query, the actual column name needs to be returned

How can I get the column names from a query

Im using Eloquent Query builder, need to re-use the column name as header

$userlst = DB::table('users')
                ->leftJoin('address', '', '=', 'address.userid')
                ->leftJoin('usergroup', '', '=', '')
                ->select (
                        ' AS online_user_id',
                        ' AS online_email_acc',
                        'users.firstname AS online_firstname',
                        'users.lastname AS online_lastname',
                ->where('', 1)

output must be


To alias the name of a column in Eloquent, you can use the select method on the query builder instance, passing an array of columns and their desired aliases as the argument. For example:

        'column_1' => 'alias_1',
        'column_2' => 'alias_2',
        // etc.

This will select the specified columns, but with the aliases you’ve provided. You can then access these columns using the aliases you’ve defined, rather than the original column names.

1 Like