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)?
@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.
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'] . '" ' : '';
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.
Thanks @rigor789 and @RoboRobok those are good solutions.
Out of interest is it at all possible to switch off notice errors?
Not in a blade template? (looks like html there....)
If so then you could use the or method
https://laravel.com/docs/5.2/blade (scroll to Echoing Data If It Exists)
<select class="{{ $selectBox['class'] or '' }} ">
further more, you can look at the https://laravelcollective.com/docs/5.2/html which has nice little html generators.
But back to your OP, disabling notices isn't a good idea :-)
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. :)
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.
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.
This is one of the many reasons I use Twig instead of Blade.
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 :)
Please sign in or create an account to participate in this conversation.