-1

I have a table in my db that includes a nullable timestamp column.

$table->timestamp('custom_timestamp')->nullable();

When the row in the db is created this column will be null. Later when the user does this specific thing I want to update this column with the timestamp when the user did this.

My question: What is the syntax for adding the custom timestamp to this column?

Pseudo code explanation:

$myObject->custom_timestamp = *AD THE CURRENT TIME*;
$myObject->save();

EDIT: If you downvote a seemingly legit question please explain your concerns in the comments

Brainmaniac
  • 2,203
  • 4
  • 29
  • 53

3 Answers3

2

You can generate the timestamp like so:

Carbon

use Carbon\Carbon;
$myObject->custom_timestamp = Carbon::now()->toDateTimeString();

Eloquent

$myObject->custom_timestamp = $myObject->freshTimestampString();
Remul
  • 7,874
  • 1
  • 13
  • 30
2

You can use Carbon library for datetime related issue. Do the following if you want to set the current time of the user's action.

$object->custom_timestamp = Carbon::now();
$object->save();

Or

$object->update(['custom_timestamp' => Carbon::now() ]);

To use Carbon in your code, add this line after <?php

use Carbon\Carbon;

If Carbon is not already in your project add the library by running the following command:

$ composer require nesbot/carbon

You don't need to use this library just for the task, but I really love this much powerful library and recommend.

Shafi
  • 1,850
  • 3
  • 22
  • 44
2

use laravel carbon. you can also format the date you want to save. read more here: carbon

use Carbon\Carbon;
$now = Carbon::now();
$myObject->custom_timestamp = $now; // 2018-10-16 22:02:28
$myObject->save();
Kapitan Teemo
  • 2,144
  • 1
  • 11
  • 25