Laravel date field automatically changes to today's date
so, I have this production schedule table:
product | date | forecasted | real | created_at | updated_at
when updating, only the 'real' field needs to be updated. so , in my view I have:
<div class="row">
<input type="hidden" name="ps_id"> // i have rendered this data through js
<div class="col-md-6 form-group">
<label>Real Quantity*</label>
<input type="number" name="real" step="any" required class="form-control">
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">{{trans('file.submit')}}</button>
</div>
controller:
public function ps_update_real(Request $request){
$lims_ps_data = ProductionScheduleDetails::find($request->ps_id);
$lims_ps_data->real = $request->real;
$lims_ps_data->save();
return redirect()->back();
}
this should only update the real field in the database but its changing the date field to today's date. Any ideas why this could be happening.
N.B when I do dd all the data are showing
It's possible that the date field is being updated by a timestamp in the updated_at field. To prevent this, you can set the date field to be a "non-updatable" field in your model.
In your ProductionScheduleDetails model, add the date field to the $guarded array:
protected $guarded = ['date'];
This will prevent the date field from being updated when you save the model.
For someone like me who have the same problem if future.
This is because of nonstandard behaviors of MySQL and MariaDB databases:
The first TIMESTAMP column in a table, if not explicitly declared with the NULL attribute or an explicit DEFAULT or ON UPDATE attribute, is automatically declared with the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP attributes.
From mysql docs.
To fix it you should explicitly set field as nullable and set default value in migration.