simmbiote's avatar

How to disable Notice errors in Laravel?

What's the best / preferred way to disable notice errors in Laravel? Can this be changed per app enviroment (APP_ENV)?

0 likes
11 replies
simmbiote's avatar

@RoboRobok ok so how do I get around notice errors with something like this?

Example: $html .= $selectBox['class'] ? ' class="' . $selectBox['class'] . '" ' : '';

This produces a 'Whoops, looks like something went wrong. page' with the error Undefined index: class

Like I said I don't want to disable all the errors. Just the notice errors.

rigor789's avatar

Well you get the error/notice when the index doesn't exist in the $selectBox array.

To get around it use something like

$html .= array_key_exists('class', $selectBox) ? ' class="' . $selectBox['class'] . '" ' : '';

Or if you prefer isset() you can do this as well:

$html .= isset($selectBox['class']) ? ' class="' . $selectBox['class'] . '" ' : '';

RoboRobok's avatar
Level 7

I would go with if. Ternary doesn't do any good here. Plus, isset is faster than array_key_exists. And more readable in my opinion. So:

if (isset($selectBox['class'])) {
    $html .= ' class="' . htmlspecialchars($selectBox['class']) . '" ';
}

Also notice I added htmlspecialchars() Lack of it can be potentially disastrous.

rigor789's avatar

Either through php.ini, or by changing the laravel exception handler in App\Exceptions\Handler I've never had to disable these so I don't know for sure. But as a general advice, you should fix code instead of hiding notices. :)

RoboRobok's avatar

Yes, it is possible through PHP: http://php.net/manual/en/function.error-reporting.php

But, again, don't do it. It's short-sightened idea to disable them. We all went through that, but one day you will realize that they really are needed. Write your code in style that explicitly shows that you are aware of some rule violation.

simmbiote's avatar

sigh... ok you guys have convinced me I'll go the long way round ;)

@nfauchelle thanks I came across that recently and I'm using it a lot.

spekkionu's avatar

This is one of the many reasons I use Twig instead of Blade.

simeonov.v's avatar

Hi, You can do that as add error_reporting in AppServiceProvider boot method:

public function boot() {
        error_reporting(E_ALL ^ E_NOTICE);
    ...
}

But, as the other guys told you - this is strictly not recommended :)

3 likes

Please or to participate in this conversation.