Bang Fady
Bang Fady

Reputation: 151

Laravel error : Call to a member function format() on string

i'm using Laravel 5.6 and i want to make an accessor in my Utility model like this

public function getRekomtekDateAttribute($value)
{
    return $value->format('d-m-Y');
}

but when i call {{ $utility->rekomtek_date }} the error, as in the title, shown

i've added this line in the same model, as in Laravel error: Call to a member function format() on string , but still no luck

/**
 * The attributes that should be mutated to dates.
 *
 * @var array
 */
protected $dates = [
    'rekomtek_date'
];

i don't know what's wrong. since i was using Laravel 5.3 this always occur -_-'

Upvotes: 0

Views: 7858

Answers (2)

nakov
nakov

Reputation: 14288

You should not pass a $value to the getter function use it like this:

public function getRekomtekDateAttribute()
{
    return $this->rekomtek_date->format('d-m-Y');
}

Upvotes: 0

Erick Patrick
Erick Patrick

Reputation: 451

Thats because you are trying to use format() in a string. You should do:

use Carbon\Carbon;
...
public function getRekomtekDateAttribute($value)
{
    return Carbon::parse($value)->format('d-m-Y');
}

Upvotes: 2

Related Questions