【发布时间】:2015-06-17 21:49:13
【问题描述】:
这里真的很奇怪。我正在通过\Console\Command 为我的网站生成一个管理员,并且在某些时候我显然会创建它。
$user = User::create([
'first_name' => 'Admin',
'last_name' => 'Inistrator',
'phone' => '',
'email' => $email,
'password' => bcrypt($password),
'role' => 'admin',
]);
我的数据库中的结果是该帐户获得了user 角色而不是admin,但如果我这样做
$user = User::create([
'first_name' => 'Admin',
'last_name' => 'Inistrator',
'phone' => '',
'email' => $email,
'password' => bcrypt($password),
'role' => 'admin',
]);
$user->role = 'admin';
$user->save();
然后它完美地工作。我怀疑 Laravel 5 在我创建一个新帐户时会做一些奇怪的事情,该帐户将与 User 模型相关的所有特征等相关联......你怎么看?
PS:这是我的迁移表
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('phone');
$table->string('email')->unique();
$table->string('password', 60);
$table->enum('role', ['user', 'business', 'admin']);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
}
【问题讨论】:
-
我认为第一步是显示执行的 SQL 查询。我还没有打开 laravel 5,所以我不知道要显示的代码行,但我发现:github.com/barryvdh/laravel-debugbar,你能复制/粘贴查询部分吗?
标签: php mysql enums models laravel-5