问题
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