php 如何从laravel的表中选择所有列名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37157270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to select all column name from a table in laravel?
提问by DMS-KH
I have tried to get all column names from a table Teller
我试图从表Teller 中获取所有列名
Function:
功能:
public function getTableColumns($tables)
{
return DB::select(DB::raw('SELECT
COLUMN_NAME,
DATA_TYPE,
COLUMN_DEFAULT
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
table_name = `Teller`'));
}
回答by Parvez Rahaman
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);
}
回答by Parvez Rahaman
Get Table NameFrom Model
从模型中获取表名
$product = new Product;
$table = $product->getTable();
print_r($table);
Get Table Column NameFrom Model
从模型中获取表列名
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function getTableColumns() {
return $this->getConnection()->getSchemaBuilder()->getColumnListing($this->getTable());
}
}
Now you will get all columns of "products" table and if you need it in controllerthen you can get it by following way :
现在您将获得“产品”表的所有列,如果您在控制器中需要它,那么您可以通过以下方式获得它:
$product = new Product;
$columns = $product->getTableColumns();
print_r($columns);
回答by Muhammad Sheraz
Just in-case if you have multiple databases connections, try following:
以防万一,如果您有多个数据库连接,请尝试以下操作:
Add in the top of your php script
在你的 php 脚本的顶部添加
use Illuminate\Support\Facades\Schema;
Retrieve anywhere in your code
检索代码中的任何位置
With Database Connection
有数据库连接
$columns = Schema::Connection('business')->getColumnListing('users'); // 'business' is your database connection
echo "<pre>";
print_r($columns);
exit();
Without Database Connection
无数据库连接
$columns = Schema::getColumnListing('users');
echo "<pre>";
print_r($columns);
exit();
回答by Eduardo Wallace
You only need extract the keys from the query response
您只需要从查询响应中提取键
array_keys(json_decode(json_encode($table[0]), true))
回答by ftrotter
As of Laravel 6.x this works:
从 Laravel 6.x 开始,这有效:
$db = DB::connection()->getPdo();
$rs = $db->query('SELECT * FROM Teller LIMIT 0');
for ($i = 0; $i < $rs->columnCount(); $i++) {
$col = $rs->getColumnMeta($i);
$columns[] = $col['name'];
}
print_r($columns);
The hint here is just to go around eloquent (which should just provide a simple way to do this, but clearly does not) and grab the PDO object and then use the answer from the same question for straight PDO access
这里的提示只是绕过 eloquent(它应该只是提供一种简单的方法来做到这一点,但显然不是)并获取 PDO 对象,然后使用同一问题的答案进行直接 PDO 访问
This will also work when there is no database selected by replacing 'Teller' with databasename.Teller
当没有通过将“Teller”替换为 databasename.Teller 来选择数据库时,这也将起作用
HTH,
哈,
-ft
-ft
回答by codedge
You could simply write:
你可以简单地写:
public function getTableColumns($tables)
{
return DB::select(
DB::raw('SELECT * FROM `Teller`')
);
}
If you have a Teller
model you can also use Teller::all();
如果你有一个Teller
模型,你也可以使用Teller::all();
Update
更新
To get all column name you can run SHOW FIELDS Teller
要获取所有列名,您可以运行 SHOW FIELDS Teller
回答by Yasin Patel
To get all raws from table:
要从表中获取所有原始数据:
$users = DB::table('users')->get();
This method will return the value of the column directly:
此方法将直接返回列的值:
$email = DB::table('users')->where('name', 'John')->value('email');
Refer:
参考:
回答by KAPIL KUMAR
You can use this
你可以用这个
DB::table('table_name')->get();