LaravelSchema::getColumnListing() return 列可以排序吗?
Can LaravelSchema::getColumnListing() return columns in order?
我想列出给定 table 中的所有列,Schema::getColumnListing()
在这方面做得很好,但是它按字母顺序返回列,而不是按创建它们的顺序返回,有办法改变吗?
这里是一个标准用户的例子table:
Table结构:
+---+-------------------+-----------------+
| 1 | id | bigint unsigned |
+---+-------------------+-----------------+
| 2 | name | varchar(255) |
+---+-------------------+-----------------+
| 3 | email | varchar(255) |
+---+-------------------+-----------------+
| 4 | email_verified_at | timestamp |
+---+-------------------+-----------------+
| 5 | password | varchar(255) |
+---+-------------------+-----------------+
| 6 | remember_token | varchar(100) |
+---+-------------------+-----------------+
| 7 | created_at | timestamp |
+---+-------------------+-----------------+
| 8 | updated_at | timestamp |
+---+-------------------+-----------------+
测试代码:
$model = new \App\Models\User;
$table = $model->getTable();
$columns = Schema::getColumnListing($table);
dd($columns);
输出:
^ array:8 [▼
0 => "created_at"
1 => "email"
2 => "email_verified_at"
3 => "id"
4 => "name"
5 => "password"
6 => "remember_token"
7 => "updated_at"
]
期望的输出:
^ array:8 [▼
0 => "id"
1 => "name"
2 => "email"
3 => "email_verified_at"
4 => "password"
5 => "remember_token"
6 => "created_at"
7 => "updated_at"
]
还没有看过它的实现,但如果它没有按顺序返回列,为什么不试试原始 mysql?
SHOW COLUMNS
FROM users;
在 DB::select()
查询中使用它并获得结果。
完整查询:
$columns = \DB::select('SHOW COLUMNS FROM users');
如果你想保持动态,使用$columns = \DB::select('SHOW COLUMNS FROM ?', [$table]);
注意:代码未经测试。
您可以使用模型属性。
$item = Model::where('id', 1)->first();
$attributes = array_keys($item->getOriginal());
// or
$attributes = array_keys($item->getAttributes());
dd($attributes);
这对我有用:
$columnObjects = $this->db::select("SELECT column_name FROM information_schema.columns WHERE table_name = 'table_name' ORDER BY ordinal_position")
;
这个 returns 列名数组作为 stdClasses,必须转换为字符串数组:
$columns = [];
foreach ($columnObjects as $key => $value) {
if (!empty($value->COLUMN_NAME)) {
$columns[] = $value->COLUMN_NAME;
}
}
注意 COLUMN_NAME 全部为大写。
我想列出给定 table 中的所有列,Schema::getColumnListing()
在这方面做得很好,但是它按字母顺序返回列,而不是按创建它们的顺序返回,有办法改变吗?
这里是一个标准用户的例子table:
Table结构:
+---+-------------------+-----------------+
| 1 | id | bigint unsigned |
+---+-------------------+-----------------+
| 2 | name | varchar(255) |
+---+-------------------+-----------------+
| 3 | email | varchar(255) |
+---+-------------------+-----------------+
| 4 | email_verified_at | timestamp |
+---+-------------------+-----------------+
| 5 | password | varchar(255) |
+---+-------------------+-----------------+
| 6 | remember_token | varchar(100) |
+---+-------------------+-----------------+
| 7 | created_at | timestamp |
+---+-------------------+-----------------+
| 8 | updated_at | timestamp |
+---+-------------------+-----------------+
测试代码:
$model = new \App\Models\User;
$table = $model->getTable();
$columns = Schema::getColumnListing($table);
dd($columns);
输出:
^ array:8 [▼
0 => "created_at"
1 => "email"
2 => "email_verified_at"
3 => "id"
4 => "name"
5 => "password"
6 => "remember_token"
7 => "updated_at"
]
期望的输出:
^ array:8 [▼
0 => "id"
1 => "name"
2 => "email"
3 => "email_verified_at"
4 => "password"
5 => "remember_token"
6 => "created_at"
7 => "updated_at"
]
还没有看过它的实现,但如果它没有按顺序返回列,为什么不试试原始 mysql?
SHOW COLUMNS
FROM users;
在 DB::select()
查询中使用它并获得结果。
完整查询:
$columns = \DB::select('SHOW COLUMNS FROM users');
如果你想保持动态,使用$columns = \DB::select('SHOW COLUMNS FROM ?', [$table]);
注意:代码未经测试。
您可以使用模型属性。
$item = Model::where('id', 1)->first();
$attributes = array_keys($item->getOriginal());
// or
$attributes = array_keys($item->getAttributes());
dd($attributes);
这对我有用:
$columnObjects = $this->db::select("SELECT column_name FROM information_schema.columns WHERE table_name = 'table_name' ORDER BY ordinal_position")
;
这个 returns 列名数组作为 stdClasses,必须转换为字符串数组:
$columns = [];
foreach ($columnObjects as $key => $value) {
if (!empty($value->COLUMN_NAME)) {
$columns[] = $value->COLUMN_NAME;
}
}
注意 COLUMN_NAME 全部为大写。