I am having an issue when updating a column in a model with 2 timestamp column, where if you update one column, it also updates the other column for some reason, not only that, it is always -8 hours. Code snippet below.
$punchIn = $user->punches()
->whereNotNull('start')
->whereNull('end')
->firstWhere('schedule_id', $schedule->id);
if($punchIn) {
// something is happening here during saving. it also updates the start column for some reason.
// this thing substracts 8 hours from the end column and updates the start column with it.
// I have been debugging this for 3 days now.
$punchIn->end = Carbon::now();
$punchIn->save(); // I also tried the other way to update, but still same thing.
$punchIn->refresh(); // this has the new value of start and end.
}
This is the migration file:
Schema::create('user_punches', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
$table->foreignId('schedule_id')->constrained('schedules')->cascadeOnDelete();
$table->timestamp('start')->nullable(); // overrides
$table->timestamp('end')->nullable(); // overrides
$table->timestamps();
});
And this is the user eloquent relationship:
class User extends Authenticatable implements MustVerifyEmail {
use HasApiTokens, HasFactory, Notifiable, HasRoles;
public function punches()
{
return $this->hasMany(UserPunch::class);
}
}
UserPunch model:
class UserPunch extends Model {
use HasFactory;
protected $fillable = [
'schedule_id',
'start',
'end',
];
public function user()
{
return $this->belongsTo(User::class);
}
}
I have been on this issue for days now. I have tried other similar cases here, like problem could be with Mysql's timestamp if not null, but still the issue is being persistent.
Anybody can point me out to the right direction? I would really appreciate any explanation on this one. Right now, I am stuck to this. My last option is to use datetime
instead of timestamp
but that would mean I would trace back the long codes and all relationships which I am trying to avoid.