簡體   English   中英

Laravel 4使用UUID作為主鍵

[英]Laravel 4 using UUID as primary key

我正在設置我的第一個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或唯一。 它被定義為varchar(36)NOT NULL。

為什么在我運行遷移時會發生這種情況?


我最初的問題是關於使用UUID,但我有意識地通過將此添加到我的Users模型解決了以防其他人看到此帖子:

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

這條線

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

必須改為

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM