Looking for database insert example According to db_insert manual page this function is deprecated and it is better to use Drupal 8 database connection to perform insert.


  Deprecated
  
  as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get a
  database connection injected into your service from the container and
  call insert() on it. For example, $injected_database->insert($table,
  $options);


Now how can I get a database connection and call insert() method?
To inject the database service add/alter the following methods in your controller class:

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
use Symfony\Component\DependencyInjection\ContainerInterface;

class PetmdController extends ControllerBase {

  protected $database;

  public function __construct(Connection $database) {
    $this-&gt;database = $database;
  }

  public static function create(ContainerInterface $container) {
    return new static(
      $container-&gt;get('database')
    );
  }

  public function foo() {
    $this-&gt;database-&gt;insert(...)-&gt;fields(...)-&gt;execute();
  }
}