title: PostgreSQL - Any way I can define a column that will cast empty string to null on insert or update?
body: <p>I am using PostgreSQL 12. I have <strong>'users'</strong> table where <strong>'email'</strong> column is nullable means optional column, however it has unique constraint as expected. This 'email' column is <strong>'citext'</strong> type column.</p>
  <p>The problem is even I send 'email' value as <strong>NULL</strong>, the database saves it as empty string instead NULL in 'email' column during insert. Is there any way I can cast empty string to NULL for 'email' column on insert or update? Because it has unique constraint and it needs to be NULL if multiple users did not provide 'email' to avoid unique constraint error.</p>
  <p>I am using PHP Laravel framework. There is no mutator defined. The controller part is following:</p>
  <pre><code>    var_dump($user-&gt;email); // email is null still
  
      $success = $user-&gt;save(); // save DB changes by ORM
  
      var_dump($user-&gt;email); // is '' string now, NULL expected
  </code></pre>
  <p>I expect the casting should happens in background or by PostgreSQL itself automatically during insert or update. May be something like 'CHECK' constraint there we can define in column that will cast automatically?</p>
  <p>Thanks in advance.</p>