Laravel model item set carbon item but returns string

Viewed 616

I have added a datetime in the database schema:

$table->dateTime('send_at')->nullable();

I have set the attribute to a Carbon instance in the seeder:

$invoice->send_at = Carbon::now();

When I try to get the type of the attribute inside the controller it returns a string:

dd(gettype($data['invoices'][0]->send_at));

What is going on? And how can I be sure that it's a Carbon Object instead of a string?

1 Answers

On your model, you need to define $dates property to create Carbon instance automatically for the column :

protected $dates = ['send_at'];

On Since Laravel 8:

protected $casts = [
    'send_at' => 'datetime',
];
Related