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

Issue

I am using Laravel 5.3.

There is a field expired_at in table articles

public function store(Request $request)
{
    $data=[
         'expired_at'=>Carbon::now()->addDays(30)->endOfDay()
    ];
    $article=Article::create(array_merge($request->all(),$data));

    return redirect('/artilces');
}

view:

{{$article->expired_at->format('Y-m-d')}}

error:

Call to a member function format() on string (View: D:\wnmp\www\laravel-5-3-dev\resources\views\artiles\index.blade.php)

Why is it?

Solution

In your Article class add following property:

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

Docs

Answered By – Amit Gupta

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published