播种机不工作 laravel 5.2

Seeder not working laravel 5.2

所以考虑以下错误:

  [PDOException]                                                                                                 
  SQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column "created_at" violates not-null constraint  
  DETAIL:  Failing row contains (5, Sample Name xxx@gmail.com, xxxxxx, null, null, null).  

这是播种机:

<?php

use Illuminate\Database\Seeder;

class AdminUser extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('users')->insert([
            'name'      => 'Sample Name',
            'email'     => 'xxxx@gmail.com',
            'password'  => 'xxxxxx',
        ]);
    }
}

用户模型:

class Users extends Model {
    protected $table        = 'users';
    protected $timestamps   = true;
    protected $fillable     = ['name', 'email', 'password', 'created_at', 'updated_at'];
}

这是怎么回事?迁移,默认来自 laravel install:

<?php

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('name');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}

他们是不是漏掉了什么?我有吗?

Eloquent 模型会自动为您插入时间戳,但查询构建器不会。正如错误消息所说,它们不能为空。您有两个选择:

  1. 手动添加时间戳:

    DB::table('users')->insert([
        'name'      => 'Sample Name',
        'email'     => 'xxxx@gmail.com',
        'password'  => 'xxxxxx',
        'updated_at' => new \Carbon\Carbon,
        'created_at' => new \Carbon\Carbon
    ]);
    
  2. 或者使用您的用户模型为数据库播种,它会为您添加时间戳。

    User::create([
        'name'      => 'Sample Name',
        'email'     => 'xxxx@gmail.com',
        'password'  => 'xxxxxx'
    ]);