Laravel Schema 中的 MySQL 数据类型 SET 等价物是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24288633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:40:12  来源:igfitidea点击:

What is the MySQL datatype SET equivalent in Laravel Schema?

phpmysqldatabaselaravelschema

提问by Debiprasad

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

Laravel Schema有一个等效于表的 ENUM 命令。SET 等价于表是什么?

回答by Roman Nazarkin

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

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

}

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

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

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

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

回答by Debiprasad

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 Schema Builder 不支持列的 SET 数据类型。所以,这是一个替代解决方案,直到有人将这些代码添加到 Laravel。

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

第一步:创建表,使用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.

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

回答by iccle

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:

Roman Nazarkin 的方法几乎可以完美运行,但是表前缀存在一个小问题(此方法没有考虑到),但是使此建议与表前缀一起使用很简单:

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

回答by Stalinko

My simple once-off solution when you need to create a custom column without creating additional files to describe extended grammar. Here I add my custom type rsvp_statusesinto PostgresGrammar:

当您需要创建自定义列而不创建其他文件来描述扩展语法时,我的简单一次性解决方案。在这里,我将我的自定义类型添加rsvp_statusesPostgresGrammar

    public function up()
    {
        DB::connection()->setSchemaGrammar(new class extends PostgresGrammar {
            protected function typeRsvp_statuses(\Illuminate\Support\Fluent $column)
            {
                return 'rsvp_statuses';
            }
        });

        Schema::create('mytable', function (Blueprint $table) {
            $table->bigIncrements('id');
            //...
            $table->addColumn('rsvp_statuses', 'status');
            $table->timestamps();
        });
    }

回答by Adewumi Adedeji

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

扩展 laravel 数据库模式方法并不太难。就像 Roman 写的那样,除了扩展之外,您还可以更新您的

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

供应商/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

供应商/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 serve 以启动 Laravel。

回答by Antoine Augusti

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

根据 Laravel API,我认为使用 Schema Builder 创建集合是不可能的。

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

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