Laravel 5枚举类型在创建时失败

时间:2015-04-12 14:59:44

标签: php mysql enums models laravel-5

这里真的很奇怪。我正在通过\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');

    }

}

3 个答案:

答案 0 :(得分:4)

我发现了问题。我听了DB::getQuerylog()的问题(我也可以在答案中使用DB::listen

查询是:

    \DB::enableQueryLog();

    $user = User::create([

        'first_name' => 'Admin',
        'last_name' => 'Inistrator',
        'phone' => '',

        'email' => $email,
        'password' => bcrypt($password),
        'role' => 'admin',

        ]);

    dd(\DB::getQueryLog());

结果是:

array:1 [
  0 => array:3 [
    "query" => "insert into `users` (`email`, `password`, `updated_at`, `created_at`) values (?, ?, ?, ?)"
    "bindings" => array:4 [
      0 => "klklm"
      1 => "$2y$10$RUorxAp4EbN/7TvEmdk.dudBBb0dgUWhmAvRsjXgVFubhEKbaLR4K"
      2 => "2015-04-13 10:30:36"
      3 => "2015-04-13 10:30:36"
    ]
    "time" => 3.17
 ]
]

所以我意识到我的田地被忽略了。我检查了模型,看到我忘了在$fillable

中添加字段
/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = ['name', 'email', 'password'];

我添加了其他字段,就像魅力一样。所以,如果有人遇到类似的问题,解决方案非常简单,不要忘记填充$fillable数组;)

答案 1 :(得分:1)

尝试此操作来监听查询:

DB::listen(function($sql, $bindings, $time)
{
    //
});

答案 2 :(得分:1)

可能会发生两件事,

  1. 您的$fillable值不匹配
  2. 或您的专栏未添加到protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { GridBind(); } } public void GridBind() { int Year = DateTime.Now.Year; int Month = DateTime.Now.Month; int curMonth = DateTime.DaysInMonth(Year, Month); DataTable Dt = new DataTable(); SqlDataAdapter Adp = new SqlDataAdapter("select Distinct e.EmpId,e.FirstName,e.Designation, count(a.status) as 'Present Days' ,('" + curMonth + "'-count(a.status)) as 'Absent Days','" + curMonth + "' as'Total Days' from EmployeeMaster e , attendance a where a.status='Absent' and e.EmployeeId=a.EmployeeId group by e.EmpId,e.firstName,e.Designation", sqlcon); Adp.Fill(Dt); gvattendance.DataSource = Dt; gvattendance.DataBind(); } 数组