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

时间:2014-06-18 14:48:00

标签: php mysql database laravel schema

Laravel Schema有一个等同于表的ENUM命令。什么是相当于表的SET?

7 个答案:

答案 0 :(得分:13)

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

}

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

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

答案 1 :(得分: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();
});

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

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

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

答案 2 :(得分:4)

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

答案 3 :(得分:2)

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

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

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

答案 4 :(得分:1)

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

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

答案 5 :(得分:-1)

根据Laravel API,我不认为可以使用Schema Builder创建一个集合。

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

答案 6 :(得分:-1)

扩展laravel数据库模式方法并不太难。就像罗马写的那样,你可以更新你的

,而不是扩展

供应商/ laravel /框架/ SRC /照亮/数据库/模式/ 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)."')";
}




供应商/ laravel /框架/ SRC /照亮/数据库/模式/ 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'));
    }




在此之后,按Ctrl + C终止服务器。然后输入php artisan serve启动laravel。

相关问题