Reputation: 455
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');
}
Upvotes: 6
Views: 28831
Reputation: 2138
Check laravel v5.1 on [Available Column Types]. To add your column do:
$table->timestamp('pDreg'); // TIMESTAMP equivalent for the database.
OR
$table->dateTime('pDreg'); // DATETIME equivalent for the database.
Then call date()
function on yours $date
.
$data = [
'pName' => $request['fname'],
'pAddress' => $request['address'],
'pBday' => $request['bday'],
'pPhone' => $request['phone'],
'pEcon' => $request['econ'],
'pDreg' => date('Y-m-d h:i:s')
]);
Upvotes: 0
Reputation: 2321
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
Upvotes: 6