chrisgeary92
3 years ago

Eloquent resets some columns stored as $dates to NOW()

Posted 3 years ago by chrisgeary92

Anyone else had a problem with Eloquent dates and Forge before?

I have a project that works fine on homestead, but when it's put onto forge one of the date fields (a custom fields I made called starts_at) doesn't save correctly if it's not a dirty value. It's odd, because I have an ends_at field which works fine. The code is the same for both.

I have narrowed it down as far as Eloquent save() method.. The correct dates are being parsed into fill() and if I dump out the attributes of the model after using fill() they are correct. However when I save the model, if the starts_at column has not changed from what is in the database it will save the current date/time. But this only happens on starts_at and not on my ends_at column.

Any ideas? I can't replicate this on homestead as it saves correctly, I only have the issue on Forge (using Envoyer for deployments).

Model:

protected $fillable = [
        'user_id', 'slug', 'name', 'description', 'starts_at', 'ends_at'
];

protected $dates = [
        'starts_at', 'ends_at'
];

Code for the update:

(the $starts_at and $ends_at carbon objects are fine, they contain the correct dates if i try to dump them out... Also $event is injected into controller from Event model)

$starts_at = \Carbon\Carbon::createFromFormat('d/m/Y H:i', "{$this->starts_date} {$this->starts_time}");
$ends_at = \Carbon\Carbon::createFromFormat('d/m/Y H:i', "{$this->ends_date} {$this->ends_time}");

$event->fill([
        'name' => $this->name,
        'description' => $this->description,
        'starts_at' => $starts_at,
        'ends_at' => $ends_at,
])->save();

Please sign in or create an account to participate in this conversation.