为什么时间戳col为null

时间:2019-01-21 05:26:17

标签: php laravel laravel-5

我在数据库表中添加了时间戳列,

clock_nanosleep()

并把表的模型设为true。

#define NSEC_PER_SEC 1000000000
/* Initial delay, 7.5ms */
const long start_delay_ns = 7500000;
/* Cycle time, 1ms */
const long cycle_time_ns = 1000000;
struct timespec deadline;

clock_gettime(CLOCK_MONOTONIC, &deadline);
deadline.tv_nsec += start_delay_ns;
deadline.tv_sec += deadline.tv_nsec / NSEC_PER_SEC;
deadline.tv_nsec %= NSEC_PER_SEC;

for (;;)
{
    struct timespec now;

    /* Sleep until deadline */
    while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &deadline, NULL) != 0)
        if (errno != EINTR)
            return; /* error handling here */

    pFunction(pArguments);    /* thread function: send data */

    /* Calculate next deadline */
    deadline.tv_nsec += cycle_time_ns;
    deadline.tv_sec += deadline.tv_nsec / NSEC_PER_SEC;
    deadline.tv_nsec %= NSEC_PER_SEC;

    clock_gettime(CLOCK_MONOTONIC, &now);
    if (now.tv_sec > deadline.tv_sec || (now.tv_sec == deadline.tv_sec && deadline.tv_nsec > now.tv_nsec))
        return; /* time overrun error handling here */
}

但是当我添加新数据时,时间戳列是否为空?知道为什么会这样吗? enter image description here

4 个答案:

答案 0 :(得分:1)

时间戳为null,因为在插入数据时也许您并没有雄辩地使用laravel。

$model= new model;

$model->column_one= 'John';

$model->save();

或者您可以使用

$table->timestamp('created_at')->default(\DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(\DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));

答案 1 :(得分:1)

尝试一下:

public function up()
    {
        Schema::table('allestates', function (Blueprint $table) {
             $table->created_at = $table->freshTimestamp();
        });
    }

//型号

 public $timestamps = false;

答案 2 :(得分:0)

如果您的时间戳不善用雄辩,那么它将无法正常工作,因此您可能必须在插入语句上手动设置时间戳

答案 3 :(得分:0)

您可以这样做

         DB::table('user_languages')->updateOrInsert([
             'id' => //user_id goes here
             'created_at' => Carbon::now(),
             'updated_at' => Carbon::now()
         ]);