laraveleloquentmutators

Time format of database field, help needed


Time is stored in my database as H:i:s format for a timefield. When I query I want time returned as hours:minutes without the seconds part.

I tried to set the "protected $dateFormat " setting using a Mutator. Can anyone show an example of the dateFormat setting needed? The database must remain hour:minute:seconds time settings, only the retrieved value needs to be changed for display.

protected $dateFormat = 'Y-m-d H:i:s'; // ?


Solution

  • you can make it with getters function in your model like this:

    public function getTimeAttribute($time){
       return date('h:i',strtotime($time))
     }
    

    Time in function name should be your column name in upper case