Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dixitchopra's avatar

Save Date

I have a table and with appointment_at as a column for saving date. How can I save date? I tried the following code but no luck.

$appointmentAt = '2015-05-10 10:00:00';
$newAppointment = Appointment::create([
                'user_id' => $userId,
                'appointment_at' => $appointmentAt
            ]);
0 likes
3 replies
RayRutjes's avatar
Level 4

Use Carbon

$newAppointment = Appointment::create([
                'user_id' => $userId,
                'appointment_at' => Carbon::now()
            ]);

Don't forget to import Carbon.

Don't also forget to register your column as a date, so it is always returned as is in your model:

<?php
...
class MyModel extends Eloquent {
    protected $dates = ['appointment_at'];
}
1 like
JarekTkaczyk's avatar

@dixitchopra No luck doesn't say anything.

Your code is OK, I would guess the field is not fillable, if it doesn't get saved at all. Otherwise be specific.

dixitchopra's avatar

I have to convert string into a date format to store in the database.

Please or to participate in this conversation.