How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: Fix “Class Carbon not found” in Laravel
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Learn > 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.
[mc4wp_form]
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
Most Popular
Display PHP errors — ini_set + php.ini configuration
How to Display PHP Errors
May 10, 2026
PHP convert string to uppercase — strtoupper and mb_strtoupper
How to Convert a String to Uppercase in PHP
May 10, 2026
PHP string to float conversion with cast, regex cleanup, NumberFormatter
How to Convert a String to Float in PHP
May 10, 2026
PHP merge arrays without duplicates — union operator and array_unique
How to Combine Two Arrays Without Duplicates in PHP
May 10, 2026
PHP delete array element — unset, array_splice, array_filter, array_search
How to Delete an Element from a PHP Array
May 10, 2026

You Might Also Like

WordPress wp_dequeue_style priority 9999 runs after plugin enqueues
Web Development

How to Deregister or Remove a CSS File in WordPress (wp_dequeue_style Not Working)

6 Min Read
Capitalize all words in JavaScript with a ucwords-style function
Web Development

Capitalize All Words in JavaScript (ucwords Equivalent) + First Letter Uppercase

6 Min Read
Laravel DataTables custom column search — filterColumn callback handles the search SQL
Web Development

How to Search Custom or Composite Columns in Laravel DataTables

8 Min Read
MySQL combine columns into string — CONCAT and CONCAT_WS
Web Development

How to Combine Multiple Columns into One String in MySQL

6 Min Read
How7o

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

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?