Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel模式中,MySQL数据类型集等效于什么?_Php_Mysql_Database_Laravel_Schema - Fatal编程技术网

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

Php 在Laravel模式中,MySQL数据类型集等效于什么?,php,mysql,database,laravel,schema,Php,Mysql,Database,Laravel,Schema,具有与表等效的枚举命令。与表等效的集合是什么?目前,Laravel Schema Builder不支持列的集合数据类型。因此,在有人将这些代码添加到Laravel之前,这里有一个替代解决方案 步骤1:创建表,使用枚举而不是集合 Schema::create('schools', function($table) { $table->increments('id'); $table->char('id_number', 6); $table->string

具有与表等效的枚举命令。与表等效的集合是什么?

目前,Laravel Schema Builder不支持列的集合数据类型。因此,在有人将这些代码添加到Laravel之前,这里有一个替代解决方案

步骤1:创建表,使用枚举而不是集合

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

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

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

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

由于我们没有编辑任何框架代码,
composer update
之后,此方法也会起作用。

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

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

vendor/laravel/framework/src/illumb/Database/Schema/Grammers/MysqlGrammer.php
/**
*为集合类型创建列定义。
*
*@param\illumb\Support\Fluent$column
*@返回字符串
*/
受保护功能类型(Fluent$列){
返回“set(”。内爆(“,”,$column->allowed)。“)”;

}
当您需要创建自定义列而无需创建其他文件来描述扩展语法时,我的简单一次性解决方案。在这里,我将我的自定义类型
rsvp\u状态
添加到
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();
        });
    }
支持在迁移中设置数据类型。对于最新版本,您只需使用以下函数:

// SET equivalent column named flavors
// Allowed values: strawberry , vanilla
$table->set('flavors', ['strawberry', 'vanilla']);
查看最新文档中的更多详细信息:


我这样做时出错<代码>传递给CreateTableName::{closure}()的参数1必须是ExtendedBlueprint的实例,Lightlight\Database\Schema\Blueprint的实例有什么想法吗?我确实在create回调中将Blueprint更改为ExtendedBlueprint。我找到了答案。我使用的是
Schema::create(…)
而不是$Schema->create(…)为什么不在后面添加表(使用
DB::statement
调用)?“你为什么要添加它,然后再修改它?”阿金问得好。我这样做是为了尽量减少
DB::statement
的使用。
// SET equivalent column named flavors
// Allowed values: strawberry , vanilla
$table->set('flavors', ['strawberry', 'vanilla']);