Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Laravel date column returns a string, so ->format() fails

ErrorCall to a member function format() on string

Short 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.

  • Applies to Laravel 12, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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);
  2. 2Match what you see, then jump to its fix

    1. 1get_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. 2The 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

What does the error look like?

  • Call to a member function format() on string
  • Error: 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.php

2Fix

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

Did this fix it?

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/ | head

2Fix

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

Did this fix it?

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 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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.