Chaibi Alaa
Chaibi Alaa

Reputation: 1386

Update Only Time in Laravel Eloquent Model Field DateTime

I want to update a datetime field in my database, by only changing the Time and not the date, is this possible in laravel using models ?

My researches driven me to a solution using Query method, but not using Models.

For example : Event = ["start" => "22-22-2003 12:14:00"] All I need is adding few hours, WITHOUT editing the date.

Upvotes: 0

Views: 1122

Answers (1)

peterm
peterm

Reputation: 92785

How about:

$event = Event::find($id);

$event->start = $event->start->addHours(2);
$event->save();

This assumes that your start attribute is being casted to a Carbon instance, which is usually done by adding it to the $dates property of your model.

class Event extends Model
{
    protected $dates = [
        'start',
    ];
}

Upvotes: 2

Related Questions