Check if database table exists or not using Magento 2.

Magento 2, When you deal with database table related stuff and first you want to check if a specific table exists or not in the database.

You need to use isTableExists() for getting table exists or not. using the above function you get the idea of table available or not.

Output: isTableExists() returns always boolean. true or false.

Demo:

<?php
namespace Path\To\Class;

use Magento\Framework\App\ResourceConnection;

class checkTableExists {

    const ORDER_TABLE = 'sales_order';

    /**
     * @var ResourceConnection
     */
    private $resourceConnection;

    public function __construct(
       ResourceConnection $resourceConnection
    ) {
       $this->resourceConnection = $resourceConnection;
    }

    /**
     * check table exists or not Query
     *
     * @return bool
     */
    public function isTableExistsOrNot()
    {
      $connection  = $this->resourceConnection->getConnection();
      $tableName = $connection->getTableName(self::ORDER_TABLE);

	  $isTableExist = $connection->isTableExists($tableName);

      return $isTableExist;
    }
}

You can call $this->isTableExistsOrNot() function to check sales_order table exist or not in database.

Output:
True