Answer the question
In order to leave comments, you need to log in
How to properly work with timestamp/carbon in laravel5?
I make comments. In the Comment model, in addition to the standard created_at date, there is also the created_at_parent parent date.
I added to the Comment model, protected $dates = ['created_at_parent']; so that created_at_parent is a carbon object.
By default created_at_parent == created_at.
I create a Comment object, assign $comment->created_at to $comment->created_at_parent.
$comment = new Comment();
$comment->text = $request->get('text');
$comment->created_at_parent = $comment->created_at;
$comment->save();
$comment = new Comment();
$comment->text = $request->get('text');
$comment->created_at_parent = Carbon::parse($comment->created_at);
$comment->save();
Answer the question
In order to leave comments, you need to log in
The problem was that $comment->created_at was empty before the save() method was called;
$comment = new Comment();
$comment->text = $request->get('text');
$comment->created_at = Carbon::now();
$comment->created_at_parent = $comment->created_at;
$comment->save();
I'm like this: $comment->created_at->toDateTimeString()
, in theory, you can write in the model
public function getCreated_atAttribute($value)
{
return $value->toDateTimeString();
}
then it will lead to this kind. but this is only if it is needed everywhere in this form
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question