Reputation: 10068
I have the following date mutators in my Article class:
class Article extends Eloquent{
protected $dates = ['published_at', 'end_at'];
public function getPublishedAtAttribute($value)
{
return Carbon::parse($value)->format('d M Y H:i');
}
public function getEndAtAttribute($value)
{
return Carbon::parse($value)->format('d M Y H:i');
}
}
I'm using a datetimepicker to set the default value to todays date when creating a new article as follows:
$('#published_at').datetimepicker({
showTodayButton: true,
format: 'D MMM YYYY HH:mm',
defaultDate: moment().hours(0).minute(0)
})
$('#end_at').datetimepicker({
showTodayButton: true,
format: 'D MMM YYYY HH:mm',
defaultDate: moment().add(30, 'days').hours(23).minute(59)
})
In my form the published_at and end_at input field is as follows:
{!! Form::text('published_at', null, ['id' => 'published_at', 'class' => 'form-control']) !!}
{!! Form::text('end_at', null, [ 'id' => 'end_at', 'class' => 'form-control']) !!}
However the datetime is being set to todays datetime for both input fields. I need the datetime to be set for published_at to be todays date at midnight and the end_at to be 30 days from todays datetime. It seems like the datepicker default is being overridden by the date accessor methods which is fine if its an edit operation. But when creating a new article I need it to default as per the dates defined in the datetimepicker widget.
Anyone know how to fix this?
Upvotes: 1
Views: 2682
Reputation: 10068
updated the mutator as follows:
public function getPublishedAtAttribute($value)
{
if (is_null($value))
return null;
else
return Carbon::parse($value)->format('d M Y H:i');
}
Upvotes: 3