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

I think this is the way. It will not throw error

{{ Carbon\Carbon::parse($article->expired_at)->format('Y-m-d') }}

If you use DB:: to retrieve complex data, you can´t use mutators. In this situation I use this:

{{ date('m-Y', strtotime($hora->Fecha)) }}

Or

{{ date('m-Y', strtotime($hora ?? ''->Fecha)) }}

Where "Fecha" is a datetime value.


In your Article class add following property:

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

Docs

Tags:

Php

Laravel