\Drupal::database() ->insert - Error: Call to undefined method Using ->condition()?
As described in Insert Queries, the object returned from \Drupal::database()-&gt;insert() doesn't have an condition() method. If the purpose of the code is creating a new database row if it doesn't exist, or update it if it already exists, 
the code should use the object returned from \Drupal::database()-&gt;merge(). See Merge Queries.

For example, the following code will create a new row if none of the existing rows contains the value $name for the name database column; otherwise, it will update the existing matching row. ($connection is the object returned from \Drupal::database() or a Connection object obtained via dependency injection.)

$connection-&gt;merge('example')
  -&gt;key(['name' =&gt; $name])
  -&gt;fields([
      'field1' =&gt; $value1,
      'field2' =&gt; $value2,
  ])
  -&gt;execute();


It is also possible to use a set of values when the row is created, and another set when the row is updated. 

$connection-&gt;merge('example')
  -&gt;insertFields([
      'field1' =&gt; $value1,
      'field2' =&gt; $value2,
  ])
  -&gt;updateFields([
    'field1' =&gt; $alternate1,
  ])
  -&gt;key(['name' =&gt; $name])
  -&gt;execute();


What that documentation page doesn't show is how to use multiple keys, as the following code does, using Merge::keys() instead of Merge::key().

$connection-&gt;merge('example')
  -&gt;insertFields([
      'field1' =&gt; $value1,
      'field2' =&gt; $value2,
  ])
  -&gt;updateFields([
    'field1' =&gt; $alternate1,
  ])
  -&gt;keys(['name' =&gt; $name, 'nickname' =&gt; $nickname])
  -&gt;execute();


The only limitation is that the values passed to keys() are all used to match a single database row. In other words, the last snippet I shown doesn't look for a row containing $name in the name database column or a row containing $nickname in the nickname column.

In cases more complex, where the Merge class doesn't help, you need to first check what the database table contains and take the necessary action basing on the table content (which involves writing more PHP code to check the database content).