这次我试图再次使用一个例子来解释我的问题。
我有一个“日历”表,其中包含以下字段:
date_from (TIMESTAMP)
date_to (TIMESTAMP)
dt (DATE)
status (INT)
在这里,您可以看到包含一些条目的表格
--------------------------------------------------------------------------
| id | date_from | date_to | dt | status |
--------------------------------------------------------------------------
| 1 | 2017-01-10 00:00:00 | 2017-01-10 00:00:00 | 2017-01-10 | 1 |
--------------------------------------------------------------------------
| 2 | 2017-01-11 00:00:00 | 2017-01-11 00:00:00 | 2017-01-11 | 1 |
--------------------------------------------------------------------------
| 3 | 2017-01-12 00:00:00 | 2017-01-12 00:00:00 | 2017-01-12 | 1 |
--------------------------------------------------------------------------
这就是我想要做的事情:
$dates = ['2017-01-10', '2017-01-11', '2017-01-12'];
foreach($dates as $date){
$d = Calendar::query()->where('dt', $date)->first();
$d->status = 2; //Booked
$d->save();
}
保存这些模型时,date_from
和date_to
设置为当前时间/ now(),是否有任何建议?
这是我的迁移文件:
Schema::create('calendars', function(Blueprint $table) {
$table->increments('id');
$table->timestamp('date_from');
$table->timestamp('date_to');
$table->date('dt')->nullable();
$table->tinyInteger('status')->default('1');
$table->timestamps();
});
这是我的模特:
class Calendar extends Model {
protected $fillable = [
'date_from',
'date_to',
'status'
];
protected $table = 'calendars';
public $timestamps = true;
protected $dates = ['date_from', 'date_to', 'dt'];
}
提前感谢您对此进行调查。
答案 0 :(得分:0)
Check in the database for that field you didn't set default CURRENT_TIMESTAMP.