我正在创建我的第一个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);
});