我使用的是laravel5.7,我试图在mysql db表中找到列的总数。是否有任何函数/方法来获取表或结果集中的列数?
发布于 2019-01-24 07:39:02
如果您正在寻找一种Laravel的方法,您可以使用Schema外观。
例如:
Schema::getColumnListing('posts')这将输出一个列数组。例如:
array:4 [▼
0 => "id"
1 => "content"
2 => "created_at"
3 => "updated_at"
]那么,这只是一个得到count()的问题。
或者,在一行中:
return count(Schema::getColumnListing('posts'));发布于 2019-01-24 07:44:06
首先,您应该从mysql读取来自information_schama数据库的访问权限。
在您的Laravel模型中执行此请求之后
SELECT count(*) AS nbr_colomn FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '<db_name>' AND TABLE_NAME = '<table_name>'发布于 2019-01-24 08:12:31
@vasim,您能试一下这个吗,您可以通过这样做得到所有的列名.
Schema::getColumnListing($table);https://stackoverflow.com/questions/54341423
复制相似问题