简体   繁体   English

Laravel 4使用UUID作为主键

[英]Laravel 4 using UUID as primary key

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. 我正在设置我的第一个Laravel 4应用程序,规范要求id字段为varchar(36)并且是UUID。

Using Eloquent, my migrations for a sample table looks like this: 使用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();
});

When the users table gets created, the id field is not defined as PK, or unique. 创建users表时,id字段未定义为PK或唯一。 It gets defined as varchar(36) NOT NULL. 它被定义为varchar(36)NOT NULL。

Why is this happening when I run the migration? 为什么在我运行迁移时会发生这种情况?


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: 我最初的问题是关于使用UUID,但我有意识地通过将此添加到我的Users模型解决了以防其他人看到此帖子:

protected $hidden = array('password');

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

Here is my route for adding a user (I am using a function to create the UUID): 这是我添加用户的路径(我使用的函数来创建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);
}); 

This line 这条线

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

Must be changed to 必须改为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM