Understanding the Call to a member function format() on string Error in Laravel 5.3
You are encountering a PHP error because you are trying to call a method on a variable that is no longer an object. Specifically, the error Call to a member function format() on string indicates that $article->expired_at has been cast into a string by the time your view template executes. This happens when accessing properties of Eloquent models that do not map directly to database columns.
In your controller, you are setting the expired_at attribute using Carbon:
'expired_at' => Carbon::now()->addDays(30)->endOfDay()
This returns a Carbon instance. However, when you retrieve this value in your Blade view using $article->expired_at, Laravel's Eloquent model casts the property to a native PHP string before returning it to the template. The underlying database column is stored as a DATE or TIMESTAMP type, so the model serializes the Carbon instance into a standard string format (usually Y-m-d H:i:s) upon access.
Because the view receives a string rather than a Carbon object, the call to $article->expired_at->format('Y-m-d') fails. PHP cannot invoke a method like format() on a string; it can only call methods that exist on objects.
How to Fix the View
The most immediate fix is to stop treating the retrieved value as an object and instead format the string directly using PHP's built-in DateTime::format syntax or string manipulation functions. Since Eloquent returns a date-time string, you can pass it directly to Carbon's formatting methods if you create a new instance, or simply use the native date function.
Update your Blade view to handle the string value:
{{ \Carbon\Carbon::parse($article->expired_at)->format('Y-m-d') }}
Alternatively, since the value is already a date-time string in MySQL format (Y-m-d H:i:s), you can use PHP's native date function to extract just the date part:
{{ date('Y-m-d', strtotime($article->expired_at)) }}
Both approaches ensure you are not calling methods on a raw string, but rather transforming that string into the desired format.
Why Casting Happens
This behavior is by design in Eloquent. When you define an attribute on your Article model without specifying a custom accessor or mutator, and the corresponding database column is of type date, datetime, or timestamp, Eloquent automatically casts the raw value from the database (or the Carbon instance passed during creation) into a string for serialization purposes.
If you attempt to access $article->expired_at inside a controller method that expects an object, you will find it is already a string. This makes chaining methods like .format(), .addDays(), or .startOfDay() impossible without first recreating the Carbon instance.
Preventing Future Issues with Accessors
If you prefer to keep the property as a Carbon instance in your model so that you can chain Carbon methods directly, you need to define an accessor on the Article model. This tells Eloquent not to cast the property to a string automatically.
Add the following method to your Article model:
public function getExpiredAtAttribute($value)
{
return $value; // Return the raw Carbon instance if possible, or handle the casting
}
However, there is a nuance here. If you pass a Carbon instance in the controller array $data, Eloquent will still attempt to cast it based on the column type defined in your migration. To retain the Carbon instance strictly, you might need to adjust how the date is stored or accessed. A more robust pattern for Laravel 5.3 often involves ensuring the accessor returns the raw value if you are certain the database stores a string, but usually, returning the raw string is sufficient unless you specifically need Carbon methods on every access.
If your goal is to always have a Carbon instance available in your views, consider using an explicit mutator or accessor that converts the string back to a Carbon instance:
public function getExpiredAtAttribute($value)
{
if ($value) {
return \Carbon\Carbon::parse($value);
}
return $value;
}
This ensures that $article->expired_at is always a Carbon object, allowing you to use the original syntax:
{{ $article->expired_at->format('Y-m-d') }}
Conclusion
The error arises from a mismatch between how Eloquent serializes date attributes and how your view expects them. By either adjusting your view to handle string dates or defining an accessor on your model to return a Carbon instance, you resolve the conflict. In Laravel 5.3, relying on native string formatting is often the most stable approach unless explicit type preservation is required throughout your application logic.
