Question

In my project, I can retrieve created at for each post as $post->created_at but, is there way to customize it from model?

I mean if I call $post->created_at, Laravel will return result of date("d F Y H:i", strtotime($date)).

Was it helpful?

Solution

Yes, you can.

in your POST model add the following accessor:

public function getCreatedAtAttribute($value)
 {
     return date("d F Y H:i", strtotime($value));
 }

OTHER TIPS

You Can Also Use Carbon Object

          // Motters To Use Created To data Only
      public function getCreatedAtAttribute($date)
      {
          return \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
      }

      public function getUpdatedAtAttribute($date)
      {
          return \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
      }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top