Laravel Schema hat einen Befehl für ENUM, der der Tabelle entspricht. Was ist das SET-Äquivalent zur Tabelle?Was ist der MySQL-Datentyp SET in Laravel Schema?
Antwort
Ab sofort Laravel Schema Builder nicht SET-Datentyp für Spalte unterstützen. Also, hier ist eine alternative Lösung, bis jemand diesen Code zu Laravel hinzufügt.
Schritt 1: Erstellen Sie die Tabelle, verwenden Sie ENUM anstelle von 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();
});
Jetzt ändern Sie ENUM zu SET.
$table_prefix = DB::getTablePrefix();
DB::statement("ALTER TABLE `" . $table_prefix . "schools` CHANGE `level` `level` SET('Preschool','Kindergarten','Primary','Secondary');");
Wenn Sie eine bessere Lösung haben, dann lassen Sie es mich bitte wissen.
Gemäß der Laravel-API glaube ich nicht, dass es möglich ist, einen Satz mit dem Schema Builder zu erstellen.
Quelle: http://laravel.com/api/class-Illuminate.Database.Schema.Blueprint.html
Schritt 1. Standardklassen erweitern (fügen Sie diesen Code in Ihre Migrationsdatei nach use
Abschnitte):
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)."')";
}
}
Schritt 2. Dann müssen wir Standardgrammatik und Bauplan Klassen unserer ändern Gewohnheit:
// 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);
});
Diese Methode wird auch nach composer update
arbeiten, weil wir keinen Framework-Code nicht bearbeitet haben.
Ich erhalte einen Fehler, wenn ich dies tue. 'Argument 1, das an CreateTableName :: {closure}() übergeben wurde, muss eine Instanz von ExtendedBlueprint sein, Instanz von Illuminate \ Database \ Schema \ Blueprint gegeben" Irgendwelche Gedanken? Ich habe Blueprint in ExtendedBlueprint in meinem Create Callback geändert. – gin93r
Ich habe es herausgefunden. Ich habe 'Schema :: create (...)' anstelle von $ schema-> create (...) – gin93r
Roman Nazarkin Methode fast perfekt funktioniert jedoch gibt es ein kleines Problem mit Tabellenpräfixe (die diese Methode nicht für nicht-Konto) ist es einfach, aber diesen Vorschlag Arbeit mit Tabellenpräfixe zu machen:
$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);
});
Er arbeitete für mich! – markdwhite
Warum fügen Sie die Tabelle nicht einfach danach hinzu (mit einem 'DB :: statement' Aufruf)? Warum fügst du es hinzu und änderst es dann? – ajon
@ajon Gute Frage. Ich habe das gemacht, um die Verwendung von 'DB :: statement' auf minimal zu halten. – Debiprasad