-
-
Save ronlobo/812048fcab29869bb1f4d70ed74bb213 to your computer and use it in GitHub Desktop.
Extending Laravel 5 Blueprint for MySqlConnection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
], | |
~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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')); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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