查询构建器未插入时间戳

时间:2015-10-31 13:27:12

标签: php mysql laravel query-builder

我正在使用“查询”构建器插入数据所有字段都已插入,但是像created_at和updated_at这样的时间戳没有插入它们都具有默认的0:0:0值 我的插入查询是

$id = DB::table('widgets')
            ->insertGetId(array(
                'creator' => Auth::user()->id,
                'widget_name' => $request->input('widget_name'),
                'pages' => json_encode($request->input('pages')),
                'domain' => $request->input('domain'),
                "settings" => $settings,
            ));

2 个答案:

答案 0 :(得分:51)

好的。字段created_atupdate_atdeleted_atEloquent的“部分”。您使用Query Builder =>插入不会影响这两个字段(created_atupdated_at)。您应该手动定义它:

$id = DB::table('widgets')
        ->insertGetId(array(
            'creator' => Auth::user()->id,
            'widget_name' => $request->input('widget_name'),
            'pages' => json_encode($request->input('pages')),
            'domain' => $request->input('domain'),
            "settings" => $settings,
            "created_at" =>  \Carbon\Carbon::now(), # new \Datetime()
            "updated_at" => \Carbon\Carbon::now(),  # new \Datetime()
        ));

答案 1 :(得分:3)

就我而言,我正在使用date('Y-m-d H:i:s');来获取当前的dateTime。每次都可以。

    $id = DB::table('widgets')
        ->insertGetId(array(
            'creator' => Auth::user()->id,
            'widget_name' => $request->input('widget_name'),
            'pages' => json_encode($request->input('pages')),
            'domain' => $request->input('domain'),
            "settings" => $settings,
            "created_at" =>  date('Y-m-d H:i:s'),
            "updated_at" => date('Y-m-d H:i:s'),
        ));