1

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

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, see Date Mutator

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.