Class Carbon\Carbon not found
Asked Answered
B

11

45

I recently added a package to my Laravel 4 site and now anything that uses Eloquent (or at least Eloquent with any reference to date/time) is showing a 500 error that states:

Class 'Carbon\Carbon' Not Found.

I tried running

composer install
composer update
composer dump-autoload
Beriberi answered 26/1, 2014 at 17:20 Comment(0)
F
7

Not saying this is work for you, but those are steps that usually fix Laravel, when the problem is not on your source code, of course:

cd /your/application/dir

rm bootstrap/compiled.php

rm -rf vendor

composer install --no-dev
Forbidding answered 26/1, 2014 at 17:22 Comment(1)
Thanks for the quick reply. I realized that what it needed was nesbot/carbon, I attempted to install using composer and it failed due to dependency issues. I uploaded it from my project folder and now everything works again... ughBeriberi
B
74

Yes, it can work as @oli-folkerd 's answer. However, as seen in Laracasts (Laravel 5 Fundamentals series Video 10 "forms" min 16:55), almost in top of your ControllerClass php file, just add the following (or import the class if your php editor allows you do so):

use Carbon\Carbon;

Now you can simply use Carbon

$input['published_at'] = Carbon::now();

without having to add Carbon\

Burkhalter answered 4/3, 2015 at 15:23 Comment(1)
it's inserting the date time but the time is not the local time. how can I insert local date-time??Ens
D
57

you need to add the line:

'Carbon' => 'Carbon\Carbon',

to the bottom of the 'aliases' array in app/config/app.php this will make the carbon library available everywhere in laravel.

Dockage answered 3/5, 2014 at 7:35 Comment(0)
A
21

You this class in controller of Laravel.

use Carbon\Carbon;

then you simply define the carbon command for print the current date

$date = Carbon::now(); 
Antiphonal answered 14/9, 2018 at 5:40 Comment(0)
L
15

For all updated version you just need to

use Carbon\Carbon;

and for the global use, you can add this in app.php

'Carbon' => 'Carbon\Carbon',

Lida answered 6/7, 2018 at 4:53 Comment(1)
None of the answers worked for me , but this one worked!Gillyflower
C
11

For Laravel 8.x Please add

'Carbon' => Illuminate\Support\Carbon::class,

in your app/config/app.php

under aliases

or if you only want to use it in your controller

then please add

use Illuminate\Support\Carbon;
Cedell answered 7/5, 2021 at 16:34 Comment(1)
It just bothers me why it is not already on app.php this is one of the very most common classCamail
S
8

My problem solved by just requiring nesbot/carbon just do this:

composer require nesbot/carbon
Sectional answered 1/7, 2015 at 18:48 Comment(1)
This solution worked for, am just wondering why composer update didn't resolve it initiallySwashbuckling
F
7

Not saying this is work for you, but those are steps that usually fix Laravel, when the problem is not on your source code, of course:

cd /your/application/dir

rm bootstrap/compiled.php

rm -rf vendor

composer install --no-dev
Forbidding answered 26/1, 2014 at 17:22 Comment(1)
Thanks for the quick reply. I realized that what it needed was nesbot/carbon, I attempted to install using composer and it failed due to dependency issues. I uploaded it from my project folder and now everything works again... ughBeriberi
A
3

I had this problem once when I updated a project from gitlab. The below command worked for me.

composer dump-autoload
Activate answered 20/11, 2018 at 16:18 Comment(0)
H
0

Some times specifying prefer-dist prefixed by “--” (aka “bare double dash”) at the end or suffixing at the end of create-project also matters while installing...

The below command was working fine in laravel 5.5 without getting an error

composer create-project laravel/laravel blog  "5.5.*" --prefer-dist

But when I was about to begin installing Laravel 5.6 with this below command

composer create-project laravel/laravel blog --prefer-dist

I used to get

Whoops\Exception\ErrorException : Class 'Carbon\Carbon' not found

After referring to the official Installation Documentation

composer create-project --prefer-dist laravel/laravel blog

After executing the above command there were no exceptions raised, therefore installation succeeded, thereby generating a base64 hash key

Hectocotylus answered 27/8, 2018 at 6:22 Comment(0)
N
0

In your view file,try to access to the class like this :

{{Carbon\Carbon::now()->addDays()}}
Ninurta answered 27/4, 2023 at 3:48 Comment(0)
R
0

In the problem I had, I solved it by changing the date from:

date: '{{\Carbon::today->format('Y-m-d') }}'

to

date: '{{\Carbon\Carbon::today()->format("Y-m-d") }}'

Romance answered 20/3 at 12:34 Comment(1)
While this technically works, if you frequently use Carbon from your blade views then it might be better to make Carbon a globally available alias that your blade views can access by following this other answer: stackoverflow.com/a/67438679Stereotomy

© 2022 - 2024 — McMap. All rights reserved.