Laravel 4使用UUID作为主键

6

我正在创建我的第一个Laravel 4应用程序,要求id字段为varchar(36)并且是UUID。 使用Eloquent,我的迁移样例表如下:

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或unique。它被定义为varchar(36) NOT NULL。
当我运行迁移时,为什么会发生这种情况?
我的初始问题是关于使用UUID,但我已经在我的用户模型中添加了以下内容来解决这个问题,以防其他人看到这篇文章:
protected $hidden = array('password');

protected $fillable = array('id', 'first_name', 'last_name', 'email', 'password');

以下是我的添加用户的路由(我使用一个函数来创建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);
}); 

3
"->primary" 或 "->primary()" 的翻译是: "->主要" 或 "->主要()"。 - Antonio Carlos Ribeiro
是的!把它作为答案。 - Jazzy
你使用了哪个函数来生成UUID?它是自定义的还是内置于Laravel中的? - Michael
1个回答

8

这一行

$table->string('id', 36)->primary;

必须更改为

$table->string('id', 36)->primary();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接