Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

juanpscotto commented Feb 13, 2019

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
You can’t perform that action at this time.