Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel 5 insert date and time to database

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');
}
like image 924
Kent Abrio Avatar asked Oct 03 '15 15:10

Kent Abrio


1 Answers

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

like image 148
Ihab Shoully Avatar answered Sep 22 '22 02:09

Ihab Shoully