PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 8, 2022

[FIXED] Get column names in Laravel as ordered on the Table

 March 08, 2022     eloquent, laravel     No comments   

Issue

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

You'll have to order the columns by ordinal_position:

public function getTableColumns()
{
    return $this->getConnection()->select(
        (new \Illuminate\Database\Schema\Grammars\MySqlGrammar)->compileColumnListing()
            .' order by ordinal_position',
        [$this->getConnection()->getDatabaseName(), $this->getTable()]
    );
}


Answered By - Jonas Staudenmeir
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing