Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get column names in Laravel 4?

Tags:

How can I get column names of a table in an array or object in Laravel 4, using Schema, DB, or Eloquent?

It seems that I can't find a ready to use function, maybe you have some custom implementations.

like image 549
Centurion Avatar asked Nov 13 '13 10:11

Centurion


People also ask

How do I get column names in SQL laravel?

You can get all the column names from a table using the DB facade and Schema facade. You have to call the getColumnListing() method (using DB and Schema facade) by passing the table name as an argument to get all columns from the table.

How do I get the column names of a table?

To get the column name of a table we use sp_help with the name of the object or table name. sp_columns returns all the column names of the object. The following query will return the table's column names: sp_columns @table_name = 'News'

How do I select a column in laravel?

Select specific columns with Laravel Eloquent To get all of the columns from the users table, we would use the following: $user = User::where('username', 'bobbyiliev')->get(); However, if you wanted to get only a specific column, you could pass it as an argument to the get() method.

How can I get column names from a table in MySQL using PHP?

The best way is to use the INFORMATION_SCHEMA metadata virtual database. Specifically the INFORMATION_SCHEMA. COLUMNS table... SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.


2 Answers

New Answer

At the time I gave this answer Laravel hadn't a way to do this directly, but now you can just:

$columns = Schema::getColumnListing('users'); 

Old Answer

Using attributes won't work because if you do

$model = new ModelName; 

You have no attributes set to that model and you'll get nothing.

Then there is still no real option for that, so I had to go down to the database level and this is my BaseModel:

<?php  class BaseModel extends \Eloquent {      public function getAllColumnsNames()     {         switch (DB::connection()->getConfig('driver')) {             case 'pgsql':                 $query = "SELECT column_name FROM information_schema.columns WHERE table_name = '".$this->table."'";                 $column_name = 'column_name';                 $reverse = true;                 break;              case 'mysql':                 $query = 'SHOW COLUMNS FROM '.$this->table;                 $column_name = 'Field';                 $reverse = false;                 break;              case 'sqlsrv':                 $parts = explode('.', $this->table);                 $num = (count($parts) - 1);                 $table = $parts[$num];                 $query = "SELECT column_name FROM ".DB::connection()->getConfig('database').".INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'".$table."'";                 $column_name = 'column_name';                 $reverse = false;                 break;              default:                  $error = 'Database driver not supported: '.DB::connection()->getConfig('driver');                 throw new Exception($error);                 break;         }          $columns = array();          foreach(DB::select($query) as $column)         {             $columns[] = $column->$column_name;         }          if($reverse)         {             $columns = array_reverse($columns);         }          return $columns;     }  } 

Use it doing:

$model = User::find(1);  dd( $model->getAllColumnsNames() ); 
like image 113
Antonio Carlos Ribeiro Avatar answered Sep 22 '22 11:09

Antonio Carlos Ribeiro


You may try Schema::getColumnListing('tablename'):

$columns = Schema::getColumnListing('users'); // users table dd($columns); // dump the result and die 

Result would be something like this depending on your table:

array (size=12)   0 => string 'id' (length=2)   1 => string 'role_id' (length=7)   2 => string 'first_name' (length=10)   3 => string 'last_name' (length=9)   4 => string 'email' (length=5)   5 => string 'username' (length=8)   6 => string 'password' (length=8)   7 => string 'remember_token' (length=14)   8 => string 'bio' (length=3)   9 => string 'created_at' (length=10)   10 => string 'updated_at' (length=10)   11 => string 'deleted_at' (length=10) 
like image 29
The Alpha Avatar answered Sep 23 '22 11:09

The Alpha