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

+3


source to share


3 answers


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.

+1


source


Another way to do it First parse $ this-> geburtstag-> diffInYears (Carbon :: now ())

$createdAt = Carbon::parse($this->geburtstag->diffInYears(Carbon::now()));

      



Then you can use

$suborder['payment_date'] = $createdAt->format('M d Y');

      

+1


source


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

.

+1


source







All Articles