Laravel Schema ma polecenie dla ENUM równoważne z tabelą. Czym jest SET odpowiadający tabeli?Jaki jest ekwiwalent SET typu danych MySQL w schemacie Laravel?
Odpowiedz
Jak teraz laravel Schema Builder nie obsługuje SET typ danych dla kolumny. Oto alternatywne rozwiązanie, dopóki ktoś nie doda tych kodów do Laravel.
Krok 1: Utwórz tabelę, użyj ENUM zamiast 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();
});
Teraz zmień ENUM na SET.
$table_prefix = DB::getTablePrefix();
DB::statement("ALTER TABLE `" . $table_prefix . "schools` CHANGE `level` `level` SET('Preschool','Kindergarten','Primary','Secondary');");
Jeśli masz lepsze rozwiązanie, proszę dać mi znać.
Według Laravel API, nie sądzę, że można utworzyć zestaw za pomocą Kreatora schematów.
Źródło: http://laravel.com/api/class-Illuminate.Database.Schema.Blueprint.html
Krok 1. Extend klas domyślnych (dodaj ten kod do pliku migracji po use
sekcje):
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)."')";
}
}
Krok 2. Następnie musimy zmienić domyślne klasy gramatyczne i plan na nasze zwyczaj:
// 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);
});
Ta metoda działa również po composer update
, bo nie edytować dowolny kod ramowej.
Otrzymuję błąd, gdy to robię. 'Argument 1 przekazany do CreateTableName :: {closure}() musi być instancją ExtendedBlueprint, instancją Illuminate \ Database \ Schema \ Blueprint given' Wszelkie myśli? Zmieniłem Blueprint na ExtendedBlueprint w moim wywołaniu oddzwonienia. – gin93r
Rozgryzłem to. Używałem 'Schema :: create (...)' zamiast $ schema-> create (...) – gin93r
metoda Roman Nazarkin za działa niemal idealnie jednak jest mały problem z prefiksów tabeli (których metoda ta nie uwzględnia) to jest proste jednak do tej pracy sugestia z prefiksów tabeli:
$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);
});
- 1. Jaki jest ekwiwalent tras rake w Phoenix?
- 2. jaki jest ekwiwalent C# statycznego {...} w Javie?
- 3. Jaki jest ekwiwalent WPF dla komponentów WinForms?
- 4. Jaki jest ekwiwalent C# operatora powtórzeń Perla?
- 5. Jaki jest ekwiwalent Rubiego dla Pythona setup.py?
- 6. Jaki jest ekwiwalent GWT dla setTimeout?
- 7. Jaki jest ekwiwalent Kotlina dla Java's String []?
- 8. Jaki jest ekwiwalent Spring DI InjectionPoint CDI?
- 9. Jaki jest ekwiwalent Rubinowy os.walk Pythona?
- 10. Jaki jest ekwiwalent C dla reinterpret_cast?
- 11. Jaki jest ekwiwalent ZF2 metody quoteInto() w ZF1?
- 12. Set sekwencjonowanie typu puzzle
- 13. Dodaj domyślną wartość pola typu enum w schemacie budowniczego
- 14. Jaki jest wzór cechy Laravel?
- 15. MySQL: Wielkość typu danych dziesiętnych
- 16. Jaki jest zakres SET IDENTITY_INSERT xyz ON?
- 17. Jaki jest zakres "SET foreign_key_checks = 0"?
- 18. Jaki jest najlepszy typ danych dla walut w MySQL?
- 19. Jaki jest limit auto_increment (integer) w mysql
- 20. Lokalizacja bazy danych na schemacie
- 21. Jaki jest ekwiwalent IE do przewijania przepełnionego webkita: dotknij
- 22. Jaki jest ekwiwalent C# dla Oracle PL/SQL COALESCE?
- 23. Jaki jest ekwiwalent DOS dla 1>/dev/null?
- 24. Jaki jest ekwiwalent Objective-C funkcji `map()` JS?
- 25. Ekwiwalent ShellExecute w .NET
- 26. Jaki jest najlepszy sposób konwertowania bazy danych mysql na sqlite?
- 27. Jaki jest cel typu jednostki w Rust?
- 28. Nie można utworzyć instancji typu Set
- 29. Reprezentacja macierzy w schemacie
- 30. Jaki jest najlepszy typ danych MYSQL lub Maria DB do przechowywania tokena JWT?
pracował dla mnie! – markdwhite
Dlaczego nie po prostu dodać tabeli później (za pomocą wywołania 'DB :: instrukcja")? Dlaczego ją dodajesz, a następnie zmieniasz? – ajon
@ajon Dobre pytanie. Uczyniłem tak, aby ograniczyć użycie 'DB :: statement' do minimum. – Debiprasad