How to select all column name from a table in laravel?
You can get all columns name by simply doing that...
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
public function getTableColumns($table)
{
return DB::getSchemaBuilder()->getColumnListing($table);
// OR
return Schema::getColumnListing($table);
}
How to get all columns from table by name
Eloquent method:
Table::where('name', $name)->get();
Query builder method:
DB::table('table')->where('name', $name)->get();
laravel:how to get columns name of eloquent model?
$columns = Schema::getColumnListing('users'); // users table
dd($columns); // dump the result and die
How can I get some specific columns name from laravel table. (not all columns name)
You can use the filter()
function of laravel collection, i.e.:
$columnNames = collect(DB::getSchemaBuilder()->getColumnListing('table'));
$columnNames = $columnNames->filter(function ($value, $key) {
return in_array($value, ['id', 'created_at', 'updated_at']) === false;
});
How do I get database column names in Laravel?
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() );
get all indexed column names from one specific table in laravel
You can use query builder of Laravel to get back the index columns of a specific table.
run anyway
composer update
Here a simple example:
$data = DB::select('SHOW INDEX FROM table;');
$array = [];
foreach($data as $key => $item){
$array[] = $item->Column_name; // <--- check column name
}
dd($array);
How to select specific columns in laravel eloquent
You can do it like this:
Table::select('name','surname')->where('id', 1)->get();
How to get table column names as array from model
You can use the getColumnListing()
method:
use Schema;
Schema::getColumnListing($this->getTable())
Related Topics
Using Regex to Filter Attributes in Xpath with PHP
How to Avoid Code Repetition with PHP SQL Prepared Statements
PHP Object Attribute with Dot in Name
Why PHP Iteration by Reference Returns a Duplicate Last Record
PHP Regex Word Boundary Matching in Utf-8
How to Dynamically Build Queries with Pdo
Using PHP as a Template Engine
Cannot Send Session Cookie - Headers Already Sent
Use Openssl_Encrypt to Replace Mcrypt for 3Des-Ecb Encryption
How to Dynamically Populate Options on Dropdown Lists Based on Selection in Another Dropdown
Why Session Object Destruction Failed
PHP String Manipulation: Extract Hrefs
PHP Integer and Float Comparison Mismatch