0
votes

In Eloquent when instantiating the model established by the two timestamps - created_at and updated_at with the same values. But according to the logic when you create a label should be established whether the created and the label is updated the next time you upgrade. How to fix it?

Eloquent 5.2, created_at and updated_at in db is integer (protected $dateFormat = 'U')


UPDATE - when I use the method, the properties created_at and updated_at is still updated at the same time. That is the first action - the creation, are set equal. The second action - update. Again, both fields are updated and they have the same value.

1
What do you want to achieve?Milkmannetje
updated_at in create moment set as nullMusaev Haybulla
Can you give a code example?Milkmannetje
My apologies, I made a mistake. In set mysql field set on update event.Musaev Haybulla
But, do you can answer me - in create row created_at and updated_at must be have same value or updated_at must be set null? How work this for you?..Musaev Haybulla

1 Answers

0
votes

This is how Laravel does things sometimes, not letting the updated_at null is also a way to avoid problems with not null constraints. But you can fix that for you, if you please. You probably will be able to do something like this:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model as Eloquent;

class BaseModel extends Eloquent
{
    public function save(array $options = [])
    {
        $saved = parent::save($options);

        if ($this->wasRecentlyCreated) {
            $this->updated_at = null;

            parent::save($options);
        }

        return $saved;
    }
}

And use this BaseModel in all your models