日期时间的Laravel种子不起作用

时间:2017-08-22 11:40:20

标签: php laravel seed

我有迁移表:

 public function up()
{
    Schema::create('order_statuses', function (Blueprint $table) {
        $table->integer('order_id')->unsigned();
        $table->integer('status_id')->unsigned();
        $table->dateTime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));

        $table->foreign('order_id')->references('id')->on('orders');
        $table->foreign('status_id')->references('id')->on('order_status_names');
    });
}

现在我想制作种子,但我有种子,但是当我想要有不同的日期时,我有问题。这是我的种子:

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

class OrderStatusesTableSeeder extends Seeder
{
public function run()
{
    DB::statement('SET FOREIGN_KEY_CHECKS=0;');
    DB::table('order_statuses')->truncate();
    DB::statement('SET FOREIGN_KEY_CHECKS=1;');

    DB::table('order_statuses')->insert([
            [
                'order_id' => 1,
                'status_id' => 1,
                'created_at' => \Carbon\Carbon::createFromDate(2014,07,22)->toDateTimeString()
            ],[
                'order_id' => 2,
                'status_id' => 2,
            ],[
                'order_id' => 3,
                'status_id' => 3,
            ],[
                'order_id' => 1,
                'status_id' => 2,
            ]
        ]
    );
}
}

我的终端有错误:

[Illuminate\Database\QueryException]                                         
SQLSTATE[21S01]: Insert value list does not match column list: 1136 
Column   
count doesn't match value count at row 2 (SQL: insert into 
`order_statuses`  
(`created_at`, `order_id`, `status_id`) values (2014-07-22 11:32:07, 
1, 1)  
, (2, 2), (3, 3), (1, 2))

这有可能解决它吗?碳是在LAravel或我必须安装这个?

1 个答案:

答案 0 :(得分:3)

  

[照亮\数据库\ QueryException]
  SQLSTATE [21S01]:插入值列表与列列表不匹配

表示您为最后三行传递错误的参数数量:

DB::table('order_statuses')->insert(
    [
        [
            'order_id' => 1,
            'status_id' => 1,
            'created_at' => \Carbon\Carbon::createFromDate(2014,07,22)->toDateTimeString()
        ],[
            'order_id' => 2,
            'status_id' => 2,
            'created_at' => \Carbon\Carbon::createFromDate(2014,07,22)->toDateTimeString()
        ],[
            'order_id' => 3,
            'status_id' => 3,
            'created_at' => \Carbon\Carbon::createFromDate(2014,07,22)->toDateTimeString()
        ],[
            'order_id' => 1,
            'status_id' => 2,
            'created_at' => \Carbon\Carbon::createFromDate(2014,07,22)->toDateTimeString()
        ]
    ]
);

如果列允许为空,则可以将null作为created_at的值传递。