5

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

Deprecated: Carbon\Traits\Date :: getDaysFromStartOfWeek(): Implicitly marking parameter $weekStartsAt as nullable is deprecated, the explicit nullable type must be used instead in C:\laragon\www\laravel-c45\vendor\nesbot\carbon\src\Carbon\Traits\Date.php on line 1394

Deprecated: Carbon\Traits\Date :: setDaysFromStartOfWeek(): Implicitly marking parameter $weekStartsAt as nullable is deprecated, the explicit nullable type must be used instead in C:\laragon\www\laravel-c45\vendor\nesbot\carbon\src\Carbon\Traits\Date.php on line 1412

Deprecated: Carbon\Traits\Date :: utcOffset(): Implicitly marking parameter $minuteOffset as nullable is deprecated, the explicit nullable type must be used instead in C:\laragon\www\laravel- c45\vendor\nesbot\carbon\src\Carbon\Traits\Date.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.

8
  • 1
    What version of Laravel are you using? If you are still on 10.x, make sure you upgrade to v10.48.25, which has fixes for PHP 8.4. Commented Dec 18, 2024 at 6:34
  • Laravel Framework 10.48.25 Commented Dec 18, 2024 at 7:05
  • Recently, I tried to downgrade my PHP in Laragon and in Composer path, now the app works fine now. Instead of displaying errors in the webpage, the error now comes up when I try to see my laravel version php artisan --version--to put it simply, it comes up when I use php artisan commands. Commented Dec 18, 2024 at 7:16
  • This looks like a Carbon issue rather than a Laravel issue. If you have an explicit dependency of Carbon in your composer.json try removing that and also try running a composer update to get the latest carbon version which does seem to be using explicitly nullable types Commented Dec 18, 2024 at 9:27
  • 1
    @Bob. Yes, is highly likely that some libraries have not updated yet. Personally, I plan to wait at least another 2 or 3 months before I try to switch some of my applications to PHP 8.4. Commented Jan 3 at 21:03

3 Answers 3

4

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

\Carbon\Carbon::setPhpVersion(PHP_VERSION_ID);
\Carbon\Carbon::disableDeprecationWarnings();

It's up to you.

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

If you want to disable errors, warnings and deprecation notices from displaying on your rendered webpage only and in a non-intrusive way, simply add the following code to the very first line of your 'public/index.php' file:

<?php
    error_reporting(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED);

    // continue with the content of your index.php file

You will still see the notices on the console when you use artisan commands but not on your webpage. Very useful if you want your website to be presentable as soon as possible without those annoying notices.

I will not advise to disable these notices from anywhere outside your local Laravel application and especially not in your php.ini file because you won't be able to debug errors that arise when building a new Laravel application. Usually, these deprecation notices arise when your want to re-install an old Laravel application while you have a newer version of PHP and if they are not related to any PHP packages you use in the actual application, then it is not an issue.

1 Comment

I apologize for the delayed response; I've been occupied with work. I will take some time to try this out tomorrow or Sunday during my free hours. Thank you for your effort in addressing and investigating this issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.