I want to display created_at as a timestamp like this :
created_at : 1625501162
so this is my created_at column in migration:
$table->string('created_at')->Carbon::now()->timestamp;
Now I want this field to be created automatically when a new record is created and record the current time. so when i change the line to this :
$table->string('created_at')->useCurrent(Carbon::now()->timestamp);
gives me this error :
Method call is provided 1 parameters, but the method signature uses 0 parameters
so how can i fix this problem?
What other ways do you suggest for timestamp created_at and store it automatically?
If you want your column to be a timestamp you should use timestamp() instead of string(). useCurrent() doesn't take a parameter. Change
$table->string('created_at')->useCurrent(Carbon::now()->timestamp);
to
$table->timestamp('created_at')->useCurrent();
https://laravel.com/docs/8.x/migrations#column-method-timestamp
https://laravel.com/docs/8.x/migrations#column-modifiers
Edit: to always get a timestamp instead of a date you can use
protected $casts = ['created_at' => 'timestamp'];
in your Model.
Instead of string you may use timestamp() in your migration.
$table->timestamp('created_at')->useCurrent();
Also you may use casting inside model to always get expected format when receiving model.
class Something extends Model {
protected $cast = [
'created_at' => 'timestamp'
];
}
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