skip to Main Content

I encountered deprecated errors in my application and don’t know how to disable it in Laravel.

Deprecated: CarbonTraitsDate :: getDaysFromStartOfWeek(): Implicitly marking parameter $weekStartsAt as nullable is deprecated, the explicit nullable type must be used instead in
C:laragonwwwlaravel-c45vendornesbotcarbonsrcCarbonTraitsDate.php on line 1394

Deprecated: CarbonTraitsDate :: setDaysFromStartOfWeek(): Implicitly marking parameter $weekStartsAt as nullable is deprecated, the explicit nullable type must be used instead in
C:laragonwwwlaravel-c45vendornesbotcarbonsrcCarbonTraitsDate.php on line 1412

Deprecated: CarbonTraitsDate :: utcOffset(): Implicitly marking parameter $minuteOffset as nullable is deprecated, the explicit nullable type must be used instead in C:laragonwwwlaravel-
c45vendornesbotcarbonsrcCarbonTraitsDate.php on line 1481

Image: Deprecated messages in Laravel application

Currently, I’m working on Laravel project, and this project have been untouched for a week or two. In the meantime, I worked on another project and upgraded my PHP version. Now, when I’m back into this project again, these errors are displayed in the application, and I want to disable the logging in this project. If you have a way to disable the logging in Laravel, please let me know.

2

Answers


  1. You can disable deprecation warnings from PHP to fix this issue. As @ref2 mentioned you can put this in your php.ini, or in your file using

    error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
    

    or

    error_reporting(E_ALL ^ E_DEPRECATED);
    

    Source: https://stackoverflow.com/a/2803783/22557063

    Since this is a Laravel app, I would recommend placing this in your AppServiceProvider (See docs).

    If that doesn’t work, you might consider placing it at the beginning of the artisan file, e.g. here. That should solve the deprecation message being displayed when running artisan commands

    Login or Signup to reply.
  2. The deprecated messages you see are probably due to a mismatch between the PHP version and the Laravel/Carbon versions used in your project.

    On newer PHP versions (PHP 8.4.2) there are stricter rules that lead to deprecated messages when older libraries are used.

    There are two ways to fix the problem: you can update dependencies or you can just disable the messages.

    In the first case (which seems smarter to me) you can act like this

    Case 1) Update Dependencies

    First make a backup of your project (just to be safe).

    Edit your composer.json

    "require": {
        "php": "^8.1",                // or as you said 8.4.2
        "laravel/framework": "^10.0", // always and only the numeric version preceded by a "^" and nothing else more (Laravel Framework 10.48.25 = 10.48.25)
        "nesbot/carbon": "^2.0"       // use a recent version of Carbon
    }
    

    then run a

    composer update
    

    Case 2) Act on PHP

    In php.ini check the line that contains error_reporting and modify it as follows

    error_reporting = E_ALL & ~E_DEPRECATED
    

    In Laravel you can suppress errors programmatically editing the file bootstrap/app.php, better then acting on artisan file

    // be sure to add the following line before the $app is returned
    error_reporting(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED);
    

    If you want to suppress errors in development environment edit your

    app/Providers/AppServiceProvider.php

    and put this line in the boot method

    if (app()->environment('local')) {
        error_reporting(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED);
    }
    

    If you want to intervene on the Carbon library instead, you can configure Carbon to silence deprecation warnings like this, always inserting in the boot function in app/Providers/AppServiceProvider.php the following lines

    CarbonCarbon::setPhpVersion(PHP_VERSION_ID);
    CarbonCarbon::disableDeprecationWarnings();
    

    It’s up to you.

    In any case, doing this should clean up your project and stop seeing log errors.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search