'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?



Solution 1:[1]

I believe you are using mysql and you can run below code to get all row counts from your database.

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gguney