[英]how can count rows from all tables in laravel
How can I display the number of rows in all database tables at the same time?如何同时显示所有数据库表中的行数?
i dont want to write code like this我不想写这样的代码
public function index()
{
$mobiles = Mobile::count();
$users = User::where('is_admin', 'no')->count();
$users_admin = User::where('is_admin', 'yes')->count();
$phonebooks = phonebook::count();
// and many tables count in here
return view('dashboard.superAdmin.database.index', compact(
'mobiles',
'users',
'users_admin',
'phonebooks'
));
}
Is there a way to do this write in low line of codes?有没有办法用低行代码来写这个?
I believe you are using mysql and you can run below code to get all row counts from your database.我相信您正在使用 mysql 并且您可以运行以下代码以从数据库中获取所有行数。
SELECT table_name, table_rows
->FROM INFORMATION_SCHEMA.TABLES
->WHERE TABLE_SCHEMA = 'your_database_name';
It will give you list of your tables and their row counts它将为您提供表格列表及其行数
SELECT SUM(TABLE_ROWS)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'your_database_name';
Here you can get their sum在这里你可以得到他们的总和
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.