Comments (12)
Turns out Blueprint
is macroable. So we're able to add a dedicated method for creating a binary UUID column. I've pushed a branch called v2, containing this change: a3fada6
We'll be able to use this solution if there's no built in binary support once Laravel 5.6 arrives.
from laravel-binary-uuid.
I like the idea, but I don't see how we can implement this now in a non breaking way. Closing for now, but will take a look again when creating a new major version
from laravel-binary-uuid.
What would you recommend as a workaround then? Currently you can either use this package for primary keys and use unoptimised field types for other UUID fields or just don't use this package at all.
from laravel-binary-uuid.
Let's do this is in a non breaking way. Let's leave typeUuid
as it is for new and add typeUuidText
.
In the next major version we could remove the override of typeUuid
and add typeUuidBinary
.
I'd accept a PR for this.
from laravel-binary-uuid.
from laravel-binary-uuid.
There's a little bit more to it than this I'm afraid.
Adding a custom type was also the first solution I went for, but it only solves half the problem.
#15 and #16 add the type to the Grammar classes, but there's no support yet to call that method from the blueprint.
Schema::create('table_name', function (Blueprint $table) {
$table->uuid('uuid');
$table->primary('uuid');
$table->uuidText('other_uuid'); // won't work
});
You could extend Laravel's Blueprint
class to add the functionality, but that class internally is always re-created on the fly by Schema
. So you'll still never get your Blueprint
implementation because Schema
internally uses the Laravel implementation.
One possible workaround could be to override the Schema
dependency at whole, but that's can have a very big impact on the codebase. I feel this package shouldn't carry that responsibility.
This problem is the exact reason we're overriding the uuid
type on the Grammar level. I'd much more prefer to see something like this:
Schema::create('table_name', function (Blueprint $table) {
$table->uuidBinary('uuid');
});
But it's not possible.
Also note that using the ->binary
method on a blueprint actually doesn't create a BINARY
field, but rather a BLOB
field, which can't be indexed because there's no way to configure its length (which is required for MySQL indices).
The way to add normal UUID fields with the ->char
method.
Schema::create('table_name', function (Blueprint $table) {
$table->char('textual_uuid', 36);
});
The only way to really solve this issue is if there was a possibility to configure which Blueprint
class is used, which requires a change in Laravel itself.
For reference, I'm talking about this line in Laravel: https://github.com/laravel/framework/blob/5.5/src/Illuminate/Database/Schema/Builder.php#L268
Oh and about that resolver: you cannot set it from a service provider, because a Builder
is always made with new
: https://github.com/laravel/framework/blob/5.5/src/Illuminate/Database/Eloquent/Model.php#L904
from laravel-binary-uuid.
This could be a possible solution @brendt
<?php
class MysqlServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('db.connection.mysql', SpatieMySqlConnection::class);
}
}
<?php
use Illuminate\Database\MySqlConnection as BaseMySqlConnection;
use Illuminate\Database\Schema\MySqlBuilder;
class MySqlConnection extends BaseMySqlConnection
{
public function getSchemaBuilder()
{
if (is_null($this->schemaGrammar)) {
$this->useDefaultSchemaGrammar();
}
$builder = new MySqlBuilder($this);
$builder->blueprintResolver(function ($table, $callback) {
return new SpatieBlueprint($table, $callback);
});
return $builder;
}
}
<?php
use Illuminate\Database\Schema\Blueprint as BaseBlueprint;
use Illuminate\Support\Facades\DB;
class Blueprint extends BaseBlueprint
{
public function uuid()
{
return $this->addColumn('uuid', $column);
}
public function uuidBinary()
{
return $this->addColumn('uuidBinary', $column);
}
}
from laravel-binary-uuid.
Yes. Though I'd like to avoid overriding a class like MySqlConnection
. It doesn't feel right if this package starts messing with such a key component.
from laravel-binary-uuid.
agreed with @brendt , you should use $table->char('textual_uuid', 36)
to add a normal text uuid.
from laravel-binary-uuid.
FYI the possibilities to add better support in the core can be found here: laravel/ideas#645
There's no consensus though, yet.
from laravel-binary-uuid.
Mildly related question, if I wanted to extend the grammar myself to add something else, how would I do it such that it's still compatible with binary UUID? I want to add MEDIUMBLOB
support to the grammar since I only use MySQL
from laravel-binary-uuid.
Because Grammar classes are not macroable; the only possibility is to extend Spatie\BinaryUuid\MySqlGrammar
and manually register it on the connection.
$connection = app('db')->connection();
$connection->setSchemaGrammar($this->createGrammarFromConnection($connection));
See https://github.com/spatie/laravel-binary-uuid/blob/master/src/UuidServiceProvider.php#L27-L45 for the full implementation we're using.
from laravel-binary-uuid.
Related Issues (20)
- Using pivot tables help with uuid
- [Idea] Split up HasBinaryUUID trait HOT 2
- Laravel Scout: Issue with newQueryForRestoration($id) (Add Array Support) HOT 2
- Composite Key Not Working HOT 18
- with('relation') . cause utf8 encode error HOT 5
- Malformed UTF-8 characters, possibly incorrectly encoded HOT 4
- Ordered UUID for Laravel MySQL grammar query compilation via custom builder HOT 1
- Running DB::purge() is unloading extension HOT 3
- uuid() blueprint method creates a char(36) column instead of binary(16) HOT 1
- Use ID and UUID HOT 1
- Usage with Laravel Nova HOT 5
- Question - Should I still have a fk defined $casts? HOT 2
- General error: 1215 Cannot add foreign key constraint when using relations HOT 2
- Relies on static UUID factory not being changed HOT 3
- Lack of "ID" in key name results in exception HOT 1
- Can we have an incremental id (for internal relations) too? HOT 1
- Relation model saving causes "UTF-8" Error HOT 2
- Conflict with Laravel Telescope HOT 1
- Disable blueprint override HOT 1
- Looking for maintainers / abandonment notice HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-binary-uuid.