What is the STAT equivalent in Larvel format?

Laravel Schema has a command for ENUM equivalent to a table. What is a SET equivalent to a table?

+5
database php mysql schema laravel
source share
4 answers

Laravel Schema Builder does not currently support the SET data type for columns. So, here is an alternative solution until someone adds this code to Laravel.

Step 1: Create a table, use ENUM instead of 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(); }); 

Now change ENUM to 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, please let me know.

+10
source share

Step 1. Extending the classes by default (add this code to the migration file after 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 the default grammar classes and classes:

 // 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 have not edited any frame code.

+12
source share

The Roman Nazarkin method works almost perfectly, but there is a small problem with table prefixes (which this method does not take into account), however, just make this proposal work with table prefixes:

 $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); }); 
+4
source share

According to the Laravel API, I don't think you can create a set using Schema Builder.

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

-one
source share

All Articles