Mutators with two date fields
Hi,
I have in DB 3 fields, notification_option (string), expiration_date and notification_date.
The idea it´s, a user select a notification option from a select and I save the dates with the option.
Example:
public function setNotificationOptionAttribute($notification){
$this->attributes['notification_option'] = $notification;
$expiration = Carbon::parse($this->attributes['expiration_date']);
dump("Before - Expiration Date: " . $this->attributes['expiration_date']);
switch ($notification) {
case '15_days_before':
dump("15 days before");
$this->attributes['notification_date'] = $expiration->subDay(15);
dump("Expiration date: " . $expiration);
dump("Notification date: " . $this->attributes['notification_date']);
break;
default:
$this->attributes['notification_date'] = null;
break;
}
// dd();
}
The problem it´s the expiration_date change too, instead only notification_date.
The output it`s:
"Before - Expiration Date: 2017-01-17 18:56:10"
"15 days before"
"Expiration date: 2017-01-02 18:56:10"
"Notification date: 2017-01-02 18:56:10"
Thanks!
When you do this;
$this->attributes['notification_date'] = $expiration->subDay(15);
You take 15 days off the $expiration and then create another handle to the same object for the notification.
What you need to do is use the copy() function to create another object and then take the 15 days off that;
$this->attributes['notification_date'] = $expiration->copy()->subDay(15);
Fast and simple.
Thanks again for your help!
Please or to participate in this conversation.