Laravel 4使用UUID作为主键 [英] Laravel 4 using UUID as primary key
问题描述
我正在设置我的第一个Laravel 4应用,规范要求id字段为varchar(36)并为UUID.
I am setting up my first Laravel 4 app and the specs call for the id fields to be varchar(36) and be a UUID.
使用Eloquent,我对示例表的迁移看起来像这样:
Using Eloquent, my migrations for a sample table looks like this:
Schema::create('users', function($table)
{
$table->string('id', 36)->primary;
$table->string('first_name', 50);
$table->string('last_name', 50);
$table->string('email')->unique();
$table->string('password', 60);
$table->timestamps();
$table->softDeletes();
});
创建users表时,id字段未定义为PK或唯一.它被定义为varchar(36)NOT NULL.
When the users table gets created, the id field is not defined as PK, or unique. It gets defined as varchar(36) NOT NULL.
为什么在运行迁移时会发生这种情况?
Why is this happening when I run the migration?
我最初的问题是关于使用UUID的,但是我已经解决了这个问题,可以将其添加到我的Users模型中,以防其他人看到此帖子:
My original question was about using UUIDs, but I have sense solved that by adding this to my Users model in case anyone else sees this post:
protected $hidden = array('password');
protected $fillable = array('id', 'first_name', 'last_name', 'email', 'password');
这是我添加用户的途径(我正在使用一个函数来创建UUID):
Here is my route for adding a user (I am using a function to create the UUID):
Route::post('signup', function()
{
$userdata = array(
'id' => gen_uuid(),
'first_name' => Input::get('first_name'),
'last_name' => Input::get('last_name'),
'email' => Input::get('email'),
'password' => Hash::make(Input::get('password'))
);
$user = new User($userdata);
$user->save();
return View::make('login/login')->with('userdata', $userdata);
});
推荐答案
此行
$table->string('id', 36)->primary;
必须更改为
$table->string('id', 36)->primary();
这篇关于Laravel 4使用UUID作为主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!