Laravel Schema中等效的MySQL数据类型是什么?

Deb*_*sad 8 php mysql database schema laravel

Laravel Schema有一个与表相当的ENUM命令.什么是相当于表的SET?

Rom*_*kin 15

步骤1.扩展默认类(在use部分之后将此代码添加到迁移文件中):

class ExtendedBlueprint extends Blueprint {

    /**
     * Create a new set column on the table.
     *
     * @param  string  $column
     * @param  array   $allowed
     * @return \Illuminate\Support\Fluent
     */
    public function set($column, array $allowed)
    {
        return $this->addColumn('set', $column, compact('allowed'));
    }

}


class ExtendedMySqlGrammar extends Illuminate\Database\Schema\Grammars\MySqlGrammar {

    /**
     * Create the column definition for an set type.
     *
     * @param  \Illuminate\Support\Fluent  $column
     * @return string
     */
    protected function typeSet(\Illuminate\Support\Fluent $column)
    {
        return "set('".implode("', '", $column->allowed)."')";
    }

}
Run Code Online (Sandbox Code Playgroud)

步骤2.然后,我们需要将默认语法和蓝图类更改为我们的自定义:

// set new grammar class
DB::connection()->setSchemaGrammar(new ExtendedMySqlGrammar());

// get custom schema object
$schema = DB::connection()->getSchemaBuilder();

// bind new blueprint class
$schema->blueprintResolver(function($table, $callback) {
    return new ExtendedBlueprint($table, $callback);
});

// then create tables 
$schema->create('table name', function(ExtendedBlueprint $table)
{
    $table->increments('id');
    $table->text('sentence');
    $table->string('author')->nullable();
    $table->string('source')->nullable();
    $table->set('difficulty', range(1, 10)); // use our new mysql type 
    $table->boolean('enabled')->default(true);
});
Run Code Online (Sandbox Code Playgroud)

这个方法也可以用composer update,因为我们没有编辑任何框架代码.


Deb*_*sad 11

截至目前,Laravel Schema Builder不支持列的SET数据类型.所以,这是一个替代解决方案,直到有人将这些代码添加到Laravel.

第1步:创建表,使用ENUM而不是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();
});
Run Code Online (Sandbox Code Playgroud)

第2步:现在将ENUM更改为SET.

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

如果您有更好的解决方案,请告诉我.


Mad*_*iya 1

Laravel 5.8 及更高版本在迁移中支持SET数据类型。对于最新版本,您只需要使用set()函数:

// SET equivalent column named flavors
// Allowed values: strawberry , vanilla
$table->set('flavors', ['strawberry', 'vanilla']);
Run Code Online (Sandbox Code Playgroud)

在最新文档中查看更多详细信息: