Laravel 4 using UUID as primary key

倾然丶 夕夏残阳落幕 提交于 2019-12-10 13:19:41

问题


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.

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();
});

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?


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');

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);
}); 

回答1:


This line

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

Must be changed to

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


来源:https://stackoverflow.com/questions/17241660/laravel-4-using-uuid-as-primary-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!