Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 2 years ago.
0

Maybe try this:

public function getDateAttribute($value)
{
   if(isset($value)) return date('d/m/Y H:i:s', strtotime($value));
}

Note uppercase Y.

I'm not 100% sure I follow exactly what you are doing and where you are getting the error, but this might help...

Last updated 2 years ago.
0

Capital Y will show a 4 digit year, but I would like to display a 2 digit year.

The situation is this... mysql datetime is Y-m-d H:i:s. So when I update some data, I enter en entry's current datetime in a database with:

$datetime = date('Y-m-d H:i:s');

But when I retrieve my data to display on screen I dont want this datetime format, I want human readable format.

I am showing a list of results, not a single one, so I did not want to get my data to then have to run through a loop in the controller to format the date before I send it to the view. And I dont want date format in the view. So I thought getDateAttribute() can be used to format the date attribute when it gets it from the database. Thus allowing me not to run through a loop to format the data myself.

But this function then messes up the date when we update data into our database.

Last updated 2 years ago.
0

I think you need to use the setDateAttribute() to format your date back to a format the database understands

Last updated 2 years ago.
0

The thing is the getter should be nothing to do with setter. Also in my controller, I use different methods for each... e.g. index view will retrieve values from the database using all(), and getter works here, then another controller method, save() will save this in the database with save()... so what I dont understand is why getter would be called for save.

BUT I dont think it is being called... the date is not saved in the format the getter formats it to.

I will try putting in a setter to be sure, but I dont think this is how it should work, and if it does, the docs need to be made clear that the setter is needed when using a getter to ensure the data is put back into the database correctly.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

amityweb amityweb Joined 8 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.