briannesbitt / Carbon

A simple PHP API extension for DateTime.
https://carbon.nesbot.com/
MIT License
16.57k stars 1.28k forks source link

Undefined class constant 'ROUND' #2098

Closed mailstreetdevelopment closed 4 years ago

mailstreetdevelopment commented 4 years ago

Hello,

I encountered an issue with the following code: The package Carbon itself

Carbon version: 2.34.2

PHP version: 7.4.6

I expected to get:

A login interface

But I actually get:

[2020-05-28 11:04:12] production.ERROR: Undefined class constant 'ROUND' {"userId":2,"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Undefined class constant 'ROUND' at /app/vendor/nesbot/carbon/src/Carbon/CarbonInterval.php:1409)
[stacktrace]
#0 /app/vendor/nesbot/carbon/src/Carbon/CarbonInterval.php(1570): Carbon\\CarbonInterval::getRoundingMethodFromOptions()
#1 /app/vendor/nesbot/carbon/src/Carbon/Traits/Difference.php(748): Carbon\\CarbonInterval->forHumans()
#2 /app/app/Http/Controllers/AdminController.php(39): Carbon\\Carbon->diffForHumans()
#3 /app/app/Http/Controllers/AdminController.php(15): App\\Http\\Controllers\\AdminController->getRelativeDate()
#4 [internal function]: App\\Http\\Controllers\\AdminController->dashboard()
#5 /app/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array()
#6 /app/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction()
#7 /app/vendor/laravel/framework/src/Illuminate/Routing/Route.php(219): Illuminate\\Routing\\ControllerDispatcher->dispatch()
#8 /app/vendor/laravel/framework/src/Illuminate/Routing/Route.php(176): Illuminate\\Routing\\Route->runController()
#9 /app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(680): Illuminate\\Routing\\Route->run()
#10 /app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}()
#11 /app/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}()
#12 /app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle()
#13 /app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#14 /app/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(43): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}()
#15 /app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Auth\\Middleware\\Authenticate->handle()
#16 /app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#17 /app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(75): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}()
#18 /app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->handle()
#19 /app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#20 /app/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}()
#21 /app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\View\\Middleware\\ShareErrorsFromSession->handle()
#22 /app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#23 /app/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(56): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}()
#24 /app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Session\\Middleware\\StartSession->handle()
#25 /app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#26 /app/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}()
#27 /app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse->handle()
#28 /app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#29 /app/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(66): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}()
#30 /app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Cookie\\Middleware\\EncryptCookies->handle()
#31 /app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#32 /app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}()
#33 /app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(682): Illuminate\\Pipeline\\Pipeline->then()
#34 /app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(657): Illuminate\\Routing\\Router->runRouteWithinStack()
#35 /app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(623): Illuminate\\Routing\\Router->runRoute()
#36 /app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(612): Illuminate\\Routing\\Router->dispatchToRoute()
#37 /app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\\Routing\\Router->dispatch()
#38 /app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}()
#39 /app/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}()
#40 /app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Fideloper\\Proxy\\TrustProxies->handle()
#41 /app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#42 /app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}()
#43 /app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle()
#44 /app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#45 /app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}()
#46 /app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle()
#47 /app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#48 /app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}()
#49 /app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle()
#50 /app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#51 /app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(62): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}()
#52 /app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle()
#53 /app/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#54 /app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}()
#55 /app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\\Pipeline\\Pipeline->then()
#56 /app/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter()
#57 /app/public/index.php(55): Illuminate\\Foundation\\Http\\Kernel->handle()
#58 {main}
"} 

Thanks!

kylekatarnls commented 4 years ago

Hello,

We need the code and the data to inspect your problem, and it's not "The package Carbon itself", it's AdminController.php around line 39 according to your stack trace. I can't guess it.

It's the same for:

A login interface

Carbon does not provide such things. Our library only returns date stuffs. If you're working with other dependencies that can depends on Carbon, provide your composer.json and the exact steps you took to get the error.

Thanks,

kylekatarnls commented 4 years ago

For the record: the constant declaration is here: https://github.com/briannesbitt/Carbon/blob/master/src/Carbon/CarbonInterface.php#L531

And CarbonInterface.php should be loaded from your vendor directory via the composer autoload. Check your installation is not corrupted.

mailstreetdevelopment commented 4 years ago

Hi Kyle, thanks for replying

This is the AdminController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Reservation;
use App\DayTrip;
use App\InformationRequest;
use Carbon\Carbon;

class AdminController extends Controller
{
    public function dashboard(Request $request)
    {
        $latest_reservation = $this->getRelativeDate(Reservation::max('created_at'));
        $latest_information_request = $this->getRelativeDate(InformationRequest::max('created_at'));
        $information_requests = InformationRequest::orderBy('created_at', 'desc')->paginate(300);
        $day_trips = DayTrip::orderBy('starting_at', 'asc');

        if (isset($request->history)) {
            $day_trips->whereDate('starting_at', '<', date('Y-m-d'));
        } else {
            $day_trips->whereDate('starting_at', '>=', date('Y-m-d'));
        }
        $day_trips = $day_trips->get();

        return view('admin.dashboard', [
            'latest_reservation' => $latest_reservation,
            'latest_information_request' => $latest_information_request,
            'day_trips' => $day_trips, 
            'history' => $request->history,
            'information_requests' => $information_requests
        ]);
    }

    public function getRelativeDate($date_string)
    {
        Carbon::setLocale('nl');
        return Carbon::parse($date_string)->diffForHumans();
    }
}

With 'A login interface' I mean the first page of the application when you'd navigate to the URL. Im getting a 500 error and the logs give me the error I posted in the OP.

Composer.json

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "require": {
        "php": "^7.1.3",
        "fideloper/proxy": "^4.0",
        "guzzlehttp/guzzle": "^6.3",
        "laravel/framework": "5.8.*",
        "laravel/tinker": "^1.0",
        "maatwebsite/excel": "^3.1",
        "propaganistas/laravel-fakeid": "^4.0",
        "wildbit/swiftmailer-postmark": "^3.0"
    },
    "require-dev": {
        "beyondcode/laravel-dump-server": "^1.0",
        "filp/whoops": "^2.0",
        "fzaninotto/faker": "^1.4",
        "mockery/mockery": "^1.0",
        "nunomaduro/collision": "^2.0",
        "phpunit/phpunit": "^7.5"
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    }
}
kylekatarnls commented 4 years ago

I installed a 5.8 laravel app, then modified the composer.json to use yours. Then ran the part of code which implies Carbon:

    public function getRelativeDate($date_string)
    {
        Carbon::setLocale('nl');
        return Carbon::parse($date_string)->diffForHumans();
    }

Using a valid date as $date_string it worked well.

Did you check your installation as I suggested? e.g. Checking that /nesbot/carbon/src/Carbon/CarbonInterface.php is present inside your vendor and check that it was well installed in vendor/composer/autoload_classmap.php. If one or the other is missing, it means the composer installation failed and you should run composer install again to fix it.

Side note: Carbon::setLocale is not recommended in this case, you should either change globally the locale of your Laravel app (in config/app.php), or just localize the single date you handle:

    public function getRelativeDate($date_string)
    {
        return Carbon::parse($date_string)->locale('nl')->diffForHumans();
    }