I want to insert the current date(date and time) along with my form inputs on laravel 5 or 5.1 only. I have this code in PatientController.php
but I'm not sure on how to include the current date.
public function create(Patient $patient, Request $request)
{
// $post = $request->all();
$data = [
'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');
}
Laravel will automatically create two timestamps()
columns when generating a new database table via a migration
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
Edit 2:
Backup Reference : https://web.archive.org/web/20151104131317/http://laravelsnippets.com:80/snippets/managing-timestamps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With