Laravel Schema has a command for ENUM equivalent to the table. What is the SET equivalent to the table?
Currently Laravel supports four database systems: MySQL, Postgres, SQLite, and SQL Server.
The Laravel Schema class provides a database agnostic way of manipulating tables. It works well with all of the databases supported by Laravel, and has a unified API across all of these systems.
Currently, Laravel provides first-party support for five databases: MariaDB 10.3+ (Version Policy) MySQL 5.7+ (Version Policy) PostgreSQL 10.0+ (Version Policy)
How to change data type of column in laravel 9 migration ? Step 1 : Install doctrine/dbal package. Step 2 : Generate migration file. Step 3 : Open generated migration file and update.
Step 1. Extend default classes(add this code to your migration file after use
sections):
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 default grammar and blueprint classes to our custom:
// 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 did not edited any framework code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With