Php Laravel数据透视表上的Uuid

Php Laravel数据透视表上的Uuid,php,mysql,laravel,laravel-5,Php,Mysql,Laravel,Laravel 5,我正在尝试执行laravel迁移以创建透视表。我有两个模型,它们是根据多对多定义的。下面是迁移的方法 public function up() { Schema::create('api_user', function (Blueprint $table) { $table->increments('id'); $table->integer('api_id')->unsigned()->index(); $tabl

我正在尝试执行laravel迁移以创建透视表。我有两个模型,它们是根据多对多定义的。下面是迁移的方法

public function up() {
    Schema::create('api_user', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('api_id')->unsigned()->index();
        $table->foreign('api_id')->references('id')->on('apis')->onDelete('cascade');

        $table->uuid('user_uid')->unsigned()->index();
        $table->foreign('user_uid')->references('uid')->on('users')->onDelete('cascade');

        $table->timestamps();
    });
}
在执行php artisan迁移之后,我收到了sql错误

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'unsigned not null, `created_at` timestamp null, `updated_at` timestamp null) def' at line 1 (SQL: create table `api_user` (`id` int unsigned not null auto_increment primary key, `api_id` int unsigned not null, `user_uid` char(36) unsigned not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

Uuid正在数据库中存储为varch。也许varchart不能设置为无符号属性。除了Laravel-5-Generators-Extended之外,还有其他方法可以从迁移中创建透视表吗?

这就是我创建UUID的方法:

创建一个扩展蓝图

use Illuminate\Database\Schema\Blueprint;

class ExtendedBlueprint extends BluePrint{

    /**
     * Create a new uuid column on the table.
     *
     * @param  string  $column
     * @return \Illuminate\Support\Fluent
     */
    public function binary_uuid($column)
    {
        return $this->addColumn('buuid', $column);

    }
}
扩展所有要支持的语言的语法

use Illuminate\Database\Schema\Grammars\MySqlGrammar;
use Illuminate\Support\Fluent;


class MysqlExtendedGrammar extends MySqlGrammar 
{


    protected function typeBuuid(Fluent $column)
    {
        return "varbinary(16)";
    }

}

use Illuminate\Database\Schema\Grammars\PostgresGrammar;
use Illuminate\Support\Fluent;

class PostgesExtendedGrammar extends PostgresGrammar 
{


    protected function typeBuuid(Fluent $column)
    {
        return "uuid";
    }
}

use Illuminate\Support\Fluent;
use Illuminate\Database\Schema\Grammars\SQLiteGrammar;

use Illuminate\Database\Schema\Blueprint;

class SqlLiteExtendedGrammar extends SQLiteGrammar 
{

    protected function typeBuuid(Fluent $column)
    {
        return "blob";
    }
}

use Illuminate\Support\Fluent;
use Illuminate\Database\Schema\Grammars\SqlServerGrammar;
class SqlServerExtendedGrammar extends SqlServerGrammar 
{


    protected function typeBuuid(Fluent $column)
    {
        return "uniqueidentifer";
    }
}
然后创建一个模式提供程序

use Doctrine\Common\Proxy\Exception\UnexpectedValueException;
use Tschallacka\PageManager\Support\BluePrint\ExtendedBlueprint;
use Db;
use Event;

/**
 * Not so eloquent ;-)
 * @author tschallacka
 *
 */
class Stutter {

    private  $transforms = [
            'mysql'     => 'Tschallacka\PageManager\Support\Grammar\MysqlExtendedGrammar',
            'postgres'  => 'Tschallacka\PageManager\Support\Grammar\PostgesExtendedGrammar',
            'sqlite'    => 'Tschallacka\PageManager\Support\Grammar\SqlLiteExtendedGrammar',
            'sqlsrv'    => 'Tschallacka\PageManager\Support\Grammar\SqlServerExtendedGrammar',
    ];

    /**
     * Set the grammar to a certain driver
     * @param string $driver mysql, postgres, sqlite, sqlsrv, something else
     * @param string $grammar Your extended grammar class '\Foo\Bar\MysqlExtendedGrammar'
     */
    public function setGrammar($driver, $grammar) 
    {
        $this->transforms[$driver] = $grammar;  
    }

    public function getGrammar($driver) {
        if(array_key_exists($driver, $this->transforms)) {
            return $this->transforms[$driver];
        }
        throw new UnexpectedValueException("Unsupported database driver $driver\n"
                ."Please attach a listener to event tschallacka.get.binary_uuid.grammars\n"
                ."and provide an extended grammar for for generating 16 bit binary fields for UUIDs.\n"
                ."Take a look at /plugins/tschallacka/dynamicpages/support/MysqlExtendedGrammar.php");
    }

    public static function tableName($table_name) {
        $prefix = DB::connection()->getTablePrefix();
        return $prefix . $table_name;
    }
    public static function getExtendedSchema() 
    {
        $stutter = new static();
        Event::fire('tschallacka.get.binary_uuid.grammars',[$stutter]);
        $driver = DB::connection()->getConfig('driver');
        $grammar = $stutter->getGrammar($driver);

        DB::connection()->setSchemaGrammar(new $grammar());

        $schema = DB::connection()->getSchemaBuilder();
        $schema->blueprintResolver(function($table, $callback) {
            return new ExtendedBlueprint($table, $callback);
        });

        return $schema;
    }
}
然后在迁移文件中

class CreatePagesTable extends Migration
{
    public function up()
    {

        $schema = Stutter::getExtendedSchema();

        $schema->create(Stutter::tableName('pagemanager_pages'), 
            function(ExtendedBlueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');

                $table->binary_uuid('auid');

                $table->timestamps();
                $table->string('name')->nullable();
                $table->string('slug',2048)->nullable();
                $table->boolean('active')->nullable();

            });
    }

    public function down()
    {
        Schema::dropIfExists('tschallacka_pagemanager_pages');
    }
}
然后,在模型中,可以具有以下属性:

protected function getAuidAttribute() 
{
    static $uuid;
    if(isset($this->attributes['auid'])) {
        if(is_null($uuid)) {
            $uuid = Uuid::import($this->attributes['auid']);
        }
        return $uuid->string;
    }

    return null;
}

protected function beforeCreate() 
{
    $uuid = Uuid::generate(4);
    $this->auid = $uuid->bytes;
    $this->attributes['auid'] = $uuid->bytes;
}
我正在使用的UUID库是

注意


实际上,我使用的是Octobercms框架,在一些次要的实现细节上可能有所不同,但大部分内容仍然适用于laravel,因为Octobercms只是laravel之上的一个层。

uuid
是一个
char
列,所以不能取消签名。我知道,但是,这是使用uuid创建透视表的另一种方法。这是一种很好的创建透视表的方法,只需删除
uuid
列创建的
unsigned
部分<代码>无符号< /代码>仅意味着数字范围在0开始移动,而不是在中间有0个。在删除的unsinDeD删除后,我收到<代码> SqLtSt[ Hy000 ]:一般错误:1215不能添加外键约束(SQL:alter tableapi\u user`add constraint
api\u user\u uid\u foreign
外键(
user\u uid
)引用
users
uid
)关于删除级联)`