Add a End Date Automatically I have a field that is a dateTime called start_at. I'd like to be able to automatically insert a dateTime value into an end_at field that is exactly one hour later than the start_at field.
DateTime::make('Start At')->rules('required'),
Is this possible? If so, how?
You can take a look into eloquent events https://laravel.com/docs/5.7/eloquent#events .
I think you are interested in creating, and the logic will be something like this:
// in your creating event listener
$model->end_at = $model->start_at->addHour();
$model->save();
Another option is a mutator on the model
public function setStartAtAttribute($value)
{
$this->attributes['start_at'] = $value;
$this->attributes['end_at'] = Carbon::parse($value)->addHour();
}
Please sign in or create an account to participate in this conversation.