Laravel date column returns a string, so ->format() fails
Call to a member function format() on stringShort answer
format() is a Carbon method, and Eloquent only turns a column into Carbon when it is cast to a date. If the column is a string, add a datetime cast to the model. For a column you select under an alias or from a subquery, apply the cast at query time with withCasts.
Fix it fast
1Find out which cause you have
Find out what type the value really is.
php artisan tinker >>> get_debug_type(App\Models\Order::first()->shipped_at);2Match what you see, then jump to its fix
- 1
get_debug_type prints string for a column such as shipped_at, while created_at prints Illuminate\Support\CarbonThe date column is not in the model's castsFix - 2
The failing attribute is not a real column of the table, for example MAX(created_at) AS last_posted_atThe value is a subquery or aliased column added with select()Fix
- 1
What does the error look like?
Call to a member function format() on stringError: Call to a member function format() on string in resources/views/orders/show.blade.php
Cause 1 of 2
The date column is not in the model's casts
1Check
Eloquent casts created_at and updated_at to Carbon by default. Any other date column comes back as a plain string until you cast it.
grep -n "casts" -A8 app/Models/Order.php2Fix
Add a datetime cast (or immutable_datetime). You can add a format, which is used when the model is serialised.
protected function casts(): array
{
return [
'shipped_at' => 'datetime',
'birthday' => 'datetime:Y-m-d',
];
}3Verify
The value is now a Carbon instance and format() works.
php artisan tinker
>>> App\Models\Order::first()->shipped_at->format('Y-m-d');Confirmed by laravel.com
Cause 2 of 2
The value is a subquery or aliased column added with select()
1Check
The model's casts do not know about computed columns. The docs say such an attribute is a simple string.
grep -rn "selectRaw\|select(\[" app/ | head2Fix
Cast it for this query with withCasts.
$users = User::select([
'users.*',
'last_posted_at' => Post::selectRaw('MAX(created_at)')
->whereColumn('user_id', 'users.id'),
])->withCasts([
'last_posted_at' => 'datetime',
])->get();3Verify
Call format() on the attribute of the first result.
$users->first()->last_posted_at->format('d M Y');Confirmed by laravel.com
How do I stop it coming back?
- List every date column in the model's casts when you create the migration.
- Use withCasts whenever a query adds a computed date column.
- Handle nullable dates with the null-safe operator in views: $order->shipped_at?->format('Y-m-d').
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Composer says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack end
composer require maatwebsite/excel fails to install
Composer says the package needs an older PHP than the one installed
Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.
3 causes, each with a fixBack end
composer install says the lock file is not compatible and asks for update
Your lock file does not contain a compatible set of packages. Please run composer update.
composer install checked the locked packages against this machine's PHP and extensions and found a conflict.
3 causes, each with a fixBack end
composer require laravel/horizon fails: ext-pcntl is missing
laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system
Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.
3 causes, each with a fixBack end
composer require laravel/passport stops with 'requirements could not be resolved'
... but these were not loaded, likely because it conflicts with another require.
Composer found no Passport release that fits your project at once.
4 causes, each with a fixBack end
Composer says the root composer.json requires a PHP version your PHP does not satisfy
Your requirements could not be resolved to an installable set of packages.
Composer compares the php constraint in your composer.json, or in a package, with the PHP that runs Composer.
3 causes, each with a fix