Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cast time in Laravel?

Tags:

laravel

I'm trying to get a TimeString from 08:00:00 to 08:00.

I would like to cast time like this:

protected $casts = [
    'opens' => 'time:H:i',
    'closes' => 'time:H:i',
];

without using mutators like getOpensAttribute().

In my migration I'm using:

$table->time('opens')->nullable();
$table->time('closes')->nullable(); 

Is it possible?

like image 409
Philipp Mochine Avatar asked May 29 '18 12:05

Philipp Mochine


1 Answers

According to the docs, the only supported cast types are:

integer, real, float, double, string, boolean, object, array, collection, date, datetime, and timestamp.

Therefore you have to convert your time to string either before you store it or after you retrieve it from the database.

like image 131
Chris C. Avatar answered Oct 21 '22 17:10

Chris C.