Model Column Format
I changed my date property to calculate the birthdays, but now that the date is loaded in my form (I am using the Form collective) I get this like 1979-07-17 00:00:00
. The correct conclusion should be1979-07-17
protected $dates = ['geburtstag'];
public function age()
{
return $this->geburtstag->diffInYears(Carbon::now());
}
I tried to change the model like
protected $geburtstagFormat = 'Y-m-d';
but didn't help.
What am I doing wrong in this case
source to share
Why don't you just use it $model->geburtstag->format('Y-m-d')
?
You can also create mutaror in your model, for example:
public function getGeburstagDateAttribute($value) {
return $this->geburtstag->format('Y-m-d');
}
and use it like this:
$model->geburtstag_date // outputs geburtstag date in 'Y-m-d' format
To set the date format in the model, use protected $dateFormat = 'Y-m-d';
within the model.
source to share
If you do not want to store the time of birth, you must change the data type in your database to date
instead of timestamp
or datetime
. This will fix the problem automatically, since when the attribute is called on your view, this retrieval is accurately shown in the database. Otherwise, if you want to keep the database intact, you need to define a mutator in your model, for example:
public function getGeburstag($value){
return Carbon::parse($value)->toDateString();
}
This will replace the original attribute value geburtag
with its formatted values Y-m-d
.
source to share