Skip to content

Instantly share code, notes, and snippets.

@m4grio
Last active June 14, 2019 06:45
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save m4grio/acff9ca3da62b6f4317f to your computer and use it in GitHub Desktop.
Save m4grio/acff9ca3da62b6f4317f to your computer and use it in GitHub Desktop.
Extending Laravel 5 Blueprint for MySqlConnection
diff --git a/config/app.php b/config/app.php
index fa5c8c2..14ba8b3 100644
--- a/config/app.php
+++ b/config/app.php
@@ -144,6 +144,7 @@ return [
+ App\Providers\MysqlServiceProvider::class,
],
~
<?php
namespace App\Database\Schema;
use Illuminate\Database\Schema\Blueprint as ParentBlueprint;
use Illuminate\Support\Facades\DB;
/**
* Class Blueprint
*
* @package App\Database\Schema
*/
class Blueprint extends ParentBlueprint
{
/**
* Add creation and update timestamps with defaults to the table.
*
* @return void
*/
public function timestamps()
{
$this->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
$this->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP'));
}
}
<?php
namespace App\Database;
use App\Database\Schema\Blueprint;
use Illuminate\Database\MySqlConnection as ParentMySqlConnection;
use Illuminate\Database\Schema\MySqlBuilder;
/**
* Class MySqlConnection
*
* @package App\Database
*/
class MySqlConnection extends ParentMySqlConnection
{
/**
* Get a schema builder instance for the connection.
* Set {@see \App\Database\Schema\Blueprint} for connection
* Blueprint resolver
*
* @return \Illuminate\Database\Schema\MySqlBuilder
*/
public function getSchemaBuilder()
{
if (is_null($this->schemaGrammar)) {
$this->useDefaultSchemaGrammar();
}
$builder = new MySqlBuilder($this);
$builder->blueprintResolver(function ($table, $callback) {
return new Blueprint($table, $callback);
});
return $builder;
}
}
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
/**
* Class MysqlServiceProvider
*
* @package App\Providers
*/
class MysqlServiceProvider extends ServiceProvider
{
/**
* @see App\Database\MySqlConnection
*/
public function register()
{
$this->app->bind('db.connection.mysql', App\Database\MySqlConnection::class);
}
}
@juanpscotto
Copy link

Thanks!!. how Can I extend the Schema the same way you did with the Blueprint??
I'm trying to do this:

 \DB::connection()->setSchemaGrammar(new ExtendedPostgresGrammar());
        $schema = \DB::connection()->getSchemaBuilder();
        $schema->blueprintResolver(function($table, $callback) {
            return new ExtendedBlueprint($table, $callback);
        });

        $schema->create('test_montos', function (ExtendedBlueprint $table) {
            $table->increments('id');
            $table->numeric('amount')->nullable(); // CUSTOM postgres type NUMERIC added in ExtendedBlueprint
            $table->decimal('amount');
            $table->timestamps();
        });

Can you give me hand please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment