php laravel 5 将日期和时间插入数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32924205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel 5 insert date and time to database
提问by Kent Abrio
I want to insert the current date or date and time along with my form inputs... i have this code but im not sure on how to include the current date,
我想插入当前日期或日期和时间以及我的表单输入......我有这个代码但我不确定如何包含当前日期,
public function create(Patient $patient, Request $request)
{
// $post = $request->all();
$data = array(
'pName' => $request['fname'],
'pAddress' => $request['address'],
'pBday' => $request['bday'],
'pPhone' => $request['phone'],
'pEcon' => $request['econ'],
'pDreg' => dateTime('created_at')
);
$patient->insert($data);
return redirect('patient');
}
this is in the PatientController.php .. please use codes in laravel 5 or 5.1 only
这是在 PatientController.php .. 请仅使用 laravel 5 或 5.1 中的代码
回答by Ihab Shoully
Laravel will automatically create two timestamps()
columns when generating a new database table via a migration
Laraveltimestamps()
在通过迁移生成新的数据库表时会自动创建两列
Schema::create('tasks', function(Blueprint $table)
{
...
$table->timestamps();
});
Edit : If you want to manage timestamps you can do it in your model:
编辑:如果您想管理时间戳,您可以在模型中进行:
class YourModel extends Eloquent
....
public static function boot()
{
public $timestamps = false;
parent::boot();
static::creating(function($model) {
$dt = new DateTime;
$model->created_at = $dt->format('m-d-y H:i:s');
return true;
});
static::updating(function($model) {
$dt = new DateTime;
$model->updated_at = $dt->format('m-d-y H:i:s');
return true;
});
}
Reference: http://laravelsnippets.com/snippets/managing-timestamps
参考:http: //laravelsnippets.com/snippets/managing-timestamps
Edit 2:
编辑2:
Backup Reference : https://web.archive.org/web/20151104131317/http://laravelsnippets.com:80/snippets/managing-timestamps
备份参考:https: //web.archive.org/web/20151104131317/http: //laravelsnippets.com: 80/snippets/ managing-timestamps