'Get column names in Laravel as ordered on the Table

I am using the following method to get the column names of the database tables of my Laravel (v5.6.24) project. I am using mysql and it was working fine as expected. But from this week the columns names are showing as ordered by name. Previously it was showing names as in order as the actual table.

How can i get the column names in same order as the table?

/*
 * Get Table Column Names
 */
public function getTableColumns()
{
    return $this->getConnection()->getSchemaBuilder()->getColumnListing($this->getTable());
}


Solution 1:[1]

Might help someone: this is a slightly modified version of @Jonas Staudenmeir answer - but tweaked to be reusable. Just feed it the table name and it will spit out an array of the table fields.

private function getTableColumns($table_name)
{
    return DB::select(
        (new \Illuminate\Database\Schema\Grammars\MySqlGrammar)->compileColumnListing()
            .' order by ordinal_position',
        [env('DB_DATABASE'), $table_name]
    );
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Steve Goddard