Musaev Haybulla
Musaev Haybulla

Reputation: 19

Eloquent set updated_at value with creating. Ho can fix it?

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.

Upvotes: 0

Views: 910

Answers (1)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87739

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

Upvotes: 0

Related Questions