2

billing_plans table contains a default_plan column. Only one can be chosen as default. But you can edit plans in table. I want to make a binary column, 1 value equals default, 0 is not. But mysql and laravel consider that 0 value is unique too. Is it possible to make only specific value of column unique?

Migration

Schema::create('billing_plans', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('price');
        $table->boolean('default_plan')->unique()->nullable();
        $table->string('name');
        $table->timestamps();
 }
qr11
  • 443
  • 2
  • 5
  • 25

1 Answers1

1

It's not possible with Mysql ignore false value of unique constraint. You need put the column nullable and when it's false put null instead of false.

$billingPlan->default_plan = ($request->input('default_plan'))?:null;

See https://stackoverflow.com/a/4617940/2389232

Community
  • 1
  • 1
SnakeDrak
  • 3,406
  • 4
  • 28
  • 41