Skip to content

Instantly share code, notes, and snippets.

@ronlobo
Forked from m4grio/Blueprint.php
Created November 8, 2017 18:02
Show Gist options
  • Save ronlobo/812048fcab29869bb1f4d70ed74bb213 to your computer and use it in GitHub Desktop.
Save ronlobo/812048fcab29869bb1f4d70ed74bb213 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment