How would I override Laravel 5.3 sql grammar

2019-09-06 02:36发布

问题:

How would I set Laravel 5.3 to use VARCHAR over NVARCHAR for migrations on mssql? Is there even a way to do this?

This question also applies to SMALLDATETIME over DATETIME.

回答1:

I created a package that lets you do custom migrations without all the hassle of extending everything yourself.

https://github.com/shiftonelabs/laravel-nomad

Once you install the package, you just change your migration to use the new passthru method, and you can give it the definition you want for your field.

// pass definition as third parameter
$table->passthru('string', 'username', 'varchar(60)');
$table->passthru('datetime', 'expires_at', 'smalldatetime');

// or use fluent definition method
$table->passthru('string', 'username')->definition('varchar(60)');
$table->passthru('datetime', 'expires_at')->definition('smalldatetime');

// if there is no defintion, it defaults to the first parameter
$table->passthru('smalldatetime', 'expires_at');


回答2:

It's doable, but you'll probably have to create a new database driver yourself, which involves in the creation of

DatabaseManager
Connection
ConnectionFactory
Schema Builder
Schema Grammar
...

The good part is that you probably can extend all those guys from Laravel and only change the grammar class:

 /**
  * Create the column definition for a string type.
  *
  * @param  \Illuminate\Support\Fluent  $column
  * @return string
  */
 protected function typeString(Fluent $column)
 {
   return 'NVARCHAR ('.$column->length.')';
 }

An example of it in this package:

https://github.com/jacquestvanzuydam/laravel-firebird/tree/5.3-support/src/Firebird