How7o
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Reading: Fix “Class Carbon not found” in Laravel
Share
Subscribe Now
How7oHow7o
Font ResizerAa
  • Marketing
  • OS
  • Features
  • Guide
  • Complaint
  • Advertise
Search
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Follow US
Copyright © 2014-2023 Ruby Theme Ltd. All Rights Reserved.
How7o > Blog > Web Development > Fix “Class Carbon not found” in Laravel
Web Development

Fix “Class Carbon not found” in Laravel

how7o
By how7o
Last updated: May 3, 2026
6 Min Read
Laravel Carbon not found error — namespace import fix
SHARE

The laravel carbon not found error — Class 'App\Http\Controllers\Carbon' not found — is a namespace problem, not a missing package. Carbon ships with every Laravel install; the error just means PHP tried to resolve Carbon against the current controller’s namespace and didn’t find it. The fix is a single use line, and this guide also covers when to pick Carbon\Carbon versus Laravel’s own Illuminate\Support\Carbon.

Contents
  • TL;DR
  • Why the error happens
  • Fix — import Carbon
  • Carbon\Carbon vs Illuminate\Support\Carbon
  • When to use now() instead
  • Still not resolving?
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-04-23 on Laravel 11 with PHP 8.3 and Carbon 3.x.

TL;DR

Add use Carbon\Carbon; under the namespace App\Http\Controllers; line at the top of the controller. Then $date = Carbon::now(); works. Prefer use Illuminate\Support\Carbon; in Laravel-specific code — it’s a Laravel-aware subclass of the upstream Carbon and is what Eloquent’s date casts return.

Why the error happens

PHP resolves unqualified class names against the current namespace. Inside app/Http/Controllers/PostController.php, the file’s namespace is App\Http\Controllers, so writing Carbon::now() without an import makes PHP look for App\Http\Controllers\Carbon. That class doesn’t exist, and the autoloader gives up with:

Class 'App\Http\Controllers\Carbon' not found

The same thing happens for any third-party class you forget to import — Str, Arr, Log, DB. The error is always the current namespace plus the class you typed.

Fix — import Carbon

Add a use statement between the namespace line and the class declaration:

<?php

namespace App\Http\Controllers;

use Carbon\Carbon;

class PostController extends Controller
{
    public function show()
    {
        $date = Carbon::now();
        // ...
    }
}

Now the autoloader resolves Carbon to Carbon\Carbon — the package shipped with Laravel via nesbot/carbon, which Laravel declares as a direct Composer dependency.

laravel carbon not found — namespace resolution flow with and without the use statement

Carbon\Carbon vs Illuminate\Support\Carbon

There are two valid import lines:

use Carbon\Carbon;            // upstream package

use Illuminate\Support\Carbon;  // Laravel-aware subclass

Illuminate\Support\Carbon extends Carbon\Carbon and is what Eloquent’s datetime casts return for $model->created_at. For greenfield Laravel code, prefer the Laravel subclass: it gives you access to Laravel’s test-time freezing (Carbon::setTestNow(...) works on both, but the Laravel one integrates cleanly with travelTo() in tests), macros, and the serializeDate hook for customizing how dates are emitted in JSON responses. For everyday now(), addDays(), diffForHumans() usage, the two classes are interchangeable — instances of one are also instances of the other.

When to use now() instead

Laravel provides a global helper now() that returns a Carbon instance without needing any import:

$inSevenDays = now()->addDays(7);
$startOfMonth = now()->startOfMonth();

It’s the ergonomic choice for one-liners. Reach for the imported Carbon class when you need static factory methods — Carbon::parse('2026-01-01'), Carbon::createFromFormat('d/m/Y', $input), Carbon::today(), Carbon::tomorrow(). Those only exist on the class itself.

Still not resolving?

If you’ve added the use line and still see the error, regenerate the Composer autoload map and clear Laravel’s caches:

composer dump-autoload
php artisan optimize:clear

One of those almost always fixes a lingering class-not-found after the import looks correct. If it persists, check that vendor/nesbot/carbon/ exists — if not, composer install was never run or it failed partway through.

Frequently asked questions

What’s the quickest fix for laravel carbon not found?

Add use Carbon\Carbon; at the top of the file (under the namespace line), then use Carbon::now() as normal. The error happens because PHP resolves unqualified class names against the current namespace (App\Http\Controllers) first — without the use line, Carbon looks for a class that doesn’t exist there.

Should I use Carbon\Carbon or Illuminate\Support\Carbon?

Prefer Illuminate\Support\Carbon in Laravel code. It extends the upstream Carbon\Carbon class and adds a handful of Laravel-aware features (macros, the serializeDate customization hook, integration with Laravel’s test-time freezing). Both classes produce identical instances for everyday use — the Laravel subclass just gives you escape hatches if you ever need them.

Why does Carbon::now() throw a class-not-found error but now() works?

now() is a Laravel global helper function, not a class — it lives in Illuminate\Foundation\helpers.php and is always available. It internally returns a Carbon instance, so now()->addDays(7) works without any use statement. Reach for now() in one-liners; use the Carbon class import when you need Carbon::parse(...), Carbon::createFromFormat(...), or other static factories.

Do I need to install Carbon separately?

No. Carbon ships as a dependency of laravel/framework via Composer, so every Laravel app has it available out of the box. If use Carbon\Carbon still doesn’t resolve, the problem is a missing composer install or a stale autoload — run composer dump-autoload to regenerate.

Can I set a default timezone for Carbon instances?

Yes, via Laravel’s config/app.php — the timezone key is read by both PHP’s internal date functions and Carbon (through Laravel’s bootstrapping). Change it to your preferred zone (for example 'timezone' => 'Asia/Dhaka') and every new Carbon instance picks it up. For one-off conversions, call ->setTimezone('UTC') on a specific instance.

Related guides

  • How to Install Laravel on Ubuntu — fresh install gets Carbon for free via Composer.
  • How to Get Current Month Records in Eloquent Laravel — Carbon-based date ranges in Eloquent.
  • How to Get Records That Are Created Today in Laravel — whereDate with now().
  • How to Install Composer on Ubuntu — the package manager that brings Carbon in.

References

Carbon official docs: carbon.nesbot.com/docs. Laravel helpers (now, today): laravel.com/docs/helpers.

TAGGED:Laravelphptroubleshooting

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Laravel unknown column CONCAT fix — DB::raw and selectRaw bypass identifier escaping How to Fix “Unknown column ‘CONCAT'” in Laravel
Next Article Laravel call controller from another controller — app() and constructor injection patterns How to Call a Controller Method from Another Controller in Laravel
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow

Subscribe Now

Subscribe to our newsletter to get our newest articles instantly!
Most Popular
Laravel get config variable — config() helper and Config facade resolving dotted keys
How to Get Config Variables in Laravel
May 3, 2026
Laravel call controller from another controller — app() and constructor injection patterns
How to Call a Controller Method from Another Controller in Laravel
May 3, 2026
Laravel Carbon not found error — namespace import fix
Fix “Class Carbon not found” in Laravel
May 3, 2026
Laravel unknown column CONCAT fix — DB::raw and selectRaw bypass identifier escaping
How to Fix “Unknown column ‘CONCAT'” in Laravel
May 3, 2026
Laravel validate in rule — restricting input to an allow-list with in: and Rule::in
Laravel Validate Input to Specific Values (in Rule)
May 3, 2026

You Might Also Like

Install Node.js on Ubuntu — terminal with NodeSource setup_22.x curl command and Node.js hexagon icon
Web Development

How to Install Node.js on Ubuntu (22.04 & 24.04): Step-by-Step

11 Min Read
CyberPanel too many redirects Laravel error fixed by restarting LiteSpeed
Server Management

Fix CyberPanel Too Many Redirects (ERR_TOO_MANY_REDIRECTS) for Laravel Subdomain

7 Min Read
Install Laravel on Ubuntu — terminal with composer create-project command and Laravel red-pillar icon
Web Development

How to Install Laravel on Ubuntu: Step-by-Step Guide

9 Min Read
Install PHP on Ubuntu — terminal with apt install php command and stylized elephant icon
Web Development

How to Install PHP on Ubuntu (22.04 & 24.04): Step-by-Step Guide

9 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Latest News

  • SEO Audit Tool
  • Client ReferralsNew
  • Execution of SEO
  • Reporting Tool

Resouce

  • Google Search Console
  • Google Keyword Planner
  • Google OptimiseHot
  • SEO Spider

Get the Top 10 in Search!

Looking for a trustworthy service to optimize the company website?
Request a Quote
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?