Search code examples
laravelmigrationconstants

Laravel set state column


I am wanting to add a new column to my film table called status which has two options active or inactive i am wanting to set all the current films in the DB to be active by default.

My first thought is to create a library file with the states set as constants incase i ever decide to add more down the line. e.g

const active = 1;
const inactive = 2;

can i then just pass the constant in as a default in the new column?

migration file

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('films', function (Blueprint $table) {
        $table->string('status')->default(\App\Library\Status::active);
    });
}

some help would be great


Solution

  • Set datatype as an enum and set a value as active and deactive with default value active it'll automatically set active you don't need to pass it by const.

    $table->enum('status',['active','deactive'])->default('active');