What is the MySQL datatype SET equivalent in Laravel Schema?

前端 未结 6 1764
春和景丽
春和景丽 2020-12-15 10:13

Laravel Schema has a command for ENUM equivalent to the table. What is the SET equivalent to the table?

6条回答
  •  醉梦人生
    2020-12-15 11:00

    As of now Laravel Schema Builder does not support SET datatype for columns. So, here is an alternative solution until someone add those code to Laravel.

    Step 1: Create the table, use ENUM instead of SET.

    Schema::create('schools', function($table)
    {
        $table->increments('id');
        $table->char('id_number', 6);
        $table->string('school_name');
        $table->enum('level', array('Preschool', 'Kindergarten', 'Primary', 'Secondary'))->index(); // *** fix this
        $table->string('phone');
        $table->string('email');
        $table->string('location');
        $table->smallInteger('city')->unsigned()->index();
        $table->smallInteger('country')->unsigned()->index();
        $table->smallInteger('head_teacher')->unsigned()->index();
        $table->smallInteger('director')->unsigned()->index();
        $table->smallInteger('created_by')->unsigned();
        $table->smallInteger('modified_by')->unsigned();
        $table->timestamps();
    });
    

    Step 2: Now change ENUM to SET.

    $table_prefix = DB::getTablePrefix();
    DB::statement("ALTER TABLE `" . $table_prefix . "schools` CHANGE `level` `level` SET('Preschool','Kindergarten','Primary','Secondary');");
    

    If you have a better solution, then please let me know.

提交回复
热议问题