【问题标题】:Laravel 5 enum type fails while creatingLaravel 5 枚举类型在创建时失败
【发布时间】: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


【解决方案1】:

试试这个来监听查询:

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

【讨论】:

    【解决方案2】:

    我发现了问题。感谢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 数组;)

    【讨论】:

      【解决方案3】:

      可能会发生两件事,

      1. 您的enum 值不匹配
      2. 或您的列未添加到$fillable 数组中

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-01
        • 2020-03-13
        • 2022-06-22
        • 2022-01-09
        • 2021-03-17
        • 2015-09-24
        • 2010-11-26
        • 1970-01-01
        相关资源
        最近更新 更多