pieter771
pieter771

Reputation: 53

Laravel format datetime

I have in my database the date of birth of a user, but I want to format that to something like 27 March 2000. I already have the date of registered and I can format that, but if I try to format the date of birth it gives me the following error: Call to a member function format() on string

This code I used to show and format the date of birth of a user:

{{ucfirst(Auth::user()->created_at->format("M d Y"))}}

I tried to use this code for the date of birth because it is in a table with all the users:

{{ucfirst($user->birthdate->format("M d Y"))}}

This is the database structure: https://i.sstatic.net/Agchg.png

Upvotes: 1

Views: 2082

Answers (1)

tkausl
tkausl

Reputation: 14279

You need to tell Laravel that your birthdate field is a date, otherwise Laravel will just fetch it as-is and not touch it at all. You do that by adding a protected $dates to the model-class which is an array of all fields which Laravel should treat as dates.

class MyModel extends [...] {
    protected $dates = ['bithdate'];
}

This will tell Laravel that birthdate is a date and Laravel will convert it to a instance of Carbon.

Upvotes: 5

Related Questions