gpt4 book ai didi

php - Laravel 数据透视表上的 Uuid

转载 作者:行者123 更新时间:2023-11-29 07:32:41 27 4
gpt4 key购买 nike

我正在尝试执行 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 migrate 后,出现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 库是 https://github.com/webpatser/laravel-uuid

注意事项

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

关于php - Laravel 数据透视表上的 Uuid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50509336/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com