添加新行created_at字段时差为-3小时

时间:2019-10-25 10:47:08

标签: mysql laravel-5

在Kununtu 18下的Laravel 5.8 / mysql 5.7.28中,我遇到了一个问题,即添加新行created_at字段时,会充满-3小时。 在我的控制下:

public function add_hostel_enquery()
{
    $request     = request();
    $requestData = $request->all();

    $newHostelInquery                   = new HostelInquery();
    $newHostelInquery->creator_id       = ! empty($requestData['creator_id']) ? $requestData['creator_id'] : '';
    $newHostelInquery->hostel_id        = ! empty($requestData['hostel_id']) ? $requestData['hostel_id'] : '';
    $newHostelInquery->full_name        = ! empty($requestData['full_name']) ? $requestData['full_name'] : '';
    $newHostelInquery->email            = ! empty($requestData['email']) ? $requestData['email'] : '';
    $newHostelInquery->phone            = ! empty($requestData['phone']) ? $requestData['phone'] : '';
    $newHostelInquery->info             = ! empty($requestData['info']) ? $requestData['info'] : '';
    $newHostelInquery->start_date       = ! empty($requestData['start_date']) ? $requestData['start_date'] : '';
    $newHostelInquery->end_date         = ! empty($requestData['end_date']) ? $requestData['end_date'] : '';
    $newHostelInquery->request_callback = !empty($requestData['request_callback']) ? "Y" : 'N';
    try {
        DB::beginTransaction();
        $newHostelInquery->save();
        DB::commit();
    ...        

在config / app.php中:

'timezone' => 'Europe/Kiev',

并且在我的操作系统中,我具有相同的时区。

数据库创建为:

Schema::create('hostel_inqueries', function(Blueprint $table)
{
    $table->increments('id');
    ...
    $table->timestamp('created_at')->useCurrent();
    $table->timestamp('updated_at')->nullable();
    ...
});

我知道更新行后,我通过代码解决了该问题:

$hostelInquery->updated_at = Carbon::now(config('app.timezone'));
$hostelInquery->save();

如何在插入时填充created_at?

1 个答案:

答案 0 :(得分:0)

我不使用$ table-> timestamps(),就像我更喜欢设置created_at服务器一样, 所以我在mysql部分找到了决定设置:

SET GLOBAL time_zone = '+3:00';

并检查:

SELECT @@global.time_zone, @@session.time_zone;