Created
September 4, 2015 00:49
-
-
Save decnorton/73483cb78889e680046b to your computer and use it in GitHub Desktop.
Basic support for Postgres UUID columns with the Laravel 5 schema builder
This file contains hidden or 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 App\Database\Schema\PostgresGrammar; | |
use Illuminate\Database\PostgresConnection as BasePostgresConnection; | |
class PostgresConnection extends BasePostgresConnection | |
{ | |
/** | |
* Get the default schema grammar instance. | |
* | |
* @return PostgresGrammar | |
*/ | |
protected function getDefaultSchemaGrammar() | |
{ | |
return $this->withTablePrefix(new PostgresGrammar); | |
} | |
/** | |
* Get a schema builder instance for the connection. | |
* | |
* @return \Illuminate\Database\Schema\Builder | |
*/ | |
public function getSchemaBuilder() | |
{ | |
$schemaBuilder = parent::getSchemaBuilder(); | |
$schemaBuilder->blueprintResolver(function ($table, $callback) { | |
return new Blueprint($table, $callback); | |
}); | |
return $schemaBuilder; | |
} | |
} |
This file contains hidden or 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 BaseBlueprint; | |
class Blueprint extends BaseBlueprint | |
{ | |
/** | |
* Create a new UUID column on the table. | |
* | |
* @param string $column | |
* @return \Illuminate\Support\Fluent | |
*/ | |
public function uuid($column) | |
{ | |
return $this->addColumn('uuid', $column); | |
} | |
} |
This file contains hidden or 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\Grammars\PostgresGrammar as BasePostgresGrammar; | |
use Illuminate\Support\Fluent; | |
class PostgresGrammar extends BasePostgresGrammar | |
{ | |
/** | |
* Create the column definition for a UUID type. | |
* | |
* @param \Illuminate\Support\Fluent $column | |
* @return string | |
*/ | |
public function typeUuid(Fluent $column) | |
{ | |
return 'uuid'; | |
} | |
} |
This file contains hidden or 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 App\Database\PostgresConnection; | |
use Illuminate\Support\ServiceProvider; | |
class DatabaseServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Register the application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
$this->app->singleton('db.connection.pgsql', PostgresConnection::class); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment