在Laravel模式中,MySQL数据类型集等效是什么?

时间:2022-09-16 14:22:21

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

Laravel模式具有与表等效的ENUM命令。与表格等价的集合是什么?

5 个解决方案

#1


12  

Step 1. Extend default classes(add this code to your migration file after use sections):

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

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)."')";
    }

}

Step 2. Then, we need to change default grammar and blueprint classes to our custom:

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

// 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);
});

This method will also work after composer update, because we did not edited any framework code.

这个方法在composer更新之后也会工作,因为我们没有编辑任何框架代码。

#2


10  

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.

到目前为止,Laravel模式构建器不支持为列设置数据类型。所以,在有人将这些代码添加到Laravel之前,这里有一个替代方案。

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

步骤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();
});

Step 2: Now change ENUM to SET.

步骤2:现在将ENUM更改为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.

如果你有更好的解决办法,请告诉我。

#3


4  

Roman Nazarkin's method works almost perfectly however there is a small issue with table prefixes (which this method does not account for) it is simple however to make this suggestion work with table prefixes:

罗马纳扎尔金的方法几乎是完美的,但是表前缀有一个小问题(这个方法没有说明)。

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

// 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);
});

#4


0  

Extending laravel database schema methods is not too hard. Like Roman wrote, instead of extending, you can as well update your

扩展laravel数据库模式方法并不难。就像罗曼写的,与其扩展,不如更新你的

vendor/laravel/framework/src/Illuminate/Database/Schema/Grammers/MysqlGrammer.php

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

vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php

/**
     * 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'));
    }

After this, terminate your server by pressing Ctrl + C. Then type php artisan serve to start the laravel.

然后,按Ctrl + c终止服务器,然后输入php artisan来启动laravel。

#5


-1  

According to the Laravel API, I don't think it is possible to create a set using the Schema Builder.

根据Laravel API,我认为不可能使用模式构建器创建一个集合。

Source: http://laravel.com/api/class-Illuminate.Database.Schema.Blueprint.html

来源:http://laravel.com/api/class-Illuminate.Database.Schema.Blueprint.html

#1


12  

Step 1. Extend default classes(add this code to your migration file after use sections):

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

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)."')";
    }

}

Step 2. Then, we need to change default grammar and blueprint classes to our custom:

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

// 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);
});

This method will also work after composer update, because we did not edited any framework code.

这个方法在composer更新之后也会工作,因为我们没有编辑任何框架代码。

#2


10  

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.

到目前为止,Laravel模式构建器不支持为列设置数据类型。所以,在有人将这些代码添加到Laravel之前,这里有一个替代方案。

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

步骤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();
});

Step 2: Now change ENUM to SET.

步骤2:现在将ENUM更改为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.

如果你有更好的解决办法,请告诉我。

#3


4  

Roman Nazarkin's method works almost perfectly however there is a small issue with table prefixes (which this method does not account for) it is simple however to make this suggestion work with table prefixes:

罗马纳扎尔金的方法几乎是完美的,但是表前缀有一个小问题(这个方法没有说明)。

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

// 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);
});

#4


0  

Extending laravel database schema methods is not too hard. Like Roman wrote, instead of extending, you can as well update your

扩展laravel数据库模式方法并不难。就像罗曼写的,与其扩展,不如更新你的

vendor/laravel/framework/src/Illuminate/Database/Schema/Grammers/MysqlGrammer.php

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

vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php

/**
     * 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'));
    }

After this, terminate your server by pressing Ctrl + C. Then type php artisan serve to start the laravel.

然后,按Ctrl + c终止服务器,然后输入php artisan来启动laravel。

#5


-1  

According to the Laravel API, I don't think it is possible to create a set using the Schema Builder.

根据Laravel API,我认为不可能使用模式构建器创建一个集合。

Source: http://laravel.com/api/class-Illuminate.Database.Schema.Blueprint.html

来源:http://laravel.com/api/class-Illuminate.Database.Schema.Blueprint.html