A few weeks back, I tweeted a quick tip on adding unique constraints to JSON columns in Laravel:

… which translates to:

ALTER TABLE barcodes ADD serial_number VARCHAR(255) AS (JSON_UNQUOTE(unit->"$.serial_number"));
CREATE UNIQUE INDEX unique_inventoriable ON barcodes(serial_number, inventory_model);

However, the concept can also be applied to nullable columns (as MySQL ignores columns with null values from unique constraints). An example would be if you wanted to have a constraint that took into account the state of a Model with soft-deletes:

$table->boolean('was_deleted')->virtualAs('IF(`deleted_at` IS NOT NULL, TRUE, FALSE)');

… or in SQL:

ALTER TABLE barcodes ADD was_deleted BOOLEAN AS (IF(`deleted_at` IS NOT NULL, TRUE, FALSE));