Updated April 11, 2023
Introduction to Laravel Errors
Whenever a new project is activated in the Laravel framework, the error and exception handling is configured in advance for the user. Most of the applications on the web have their customized mechanisms for error handling. With the help of the particular mechanisms, they detect the errors and also log them in order to analyze the situation and the performance of the application. In Laravel too, there are mechanisms that are enabled to handle errors of various kinds that may arise eventually. Laravel logs the errors and reverts it to the user to detect the problem and resolve it.
What are Laravel Errors?
The App\Exceptions\Handler is the class where all the exceptions that are stimulated by the application are stored safely. From there they are reverted back to the user for further analysis and problem-solving. There is a Monolog library which is used by Laravel to provide the necessary support for different log handlers. Laravel is capable of configuring several handlers for the user. It gives the allowance to choose between rotating log files, a single log file and even writing them to the system log.
To identify and analyze as to how much error information is displayed to the user; you have to check the debug option which is present in the config/app.php. In a default manner, it is already set in advance with respect to the APP_DEBUG environment variable’s value. This is stored in the .env file. In order to process the local development, the user will have to configure the APP_DEBUG variable to true. But in the production environment, the user has to make sure that the value is set to false. This will save the user from the risk of exposing the sensitive configuration values to the end-users of the application.
*The APP_DEBUG variable is readily set to false in the application.
*The APP_DEBUG variable being converted to become true.
The user can configure the log option in the config/app.php file in order to determine which of the storage mechanisms Laravel must use for the application which is in progress. In order to use daily log files rather than a single log file, the log value in the app configuration needs to be set to daily.
'Log' =>'daily'
To note is the fact that Laravel is set up in default to only retain log files of a period of five days when the user uses daily log mode. But to make the necessary change and increase the number of days, the user will need to update the necessary alteration by adding log_max_files configuration value to the app configuration file.
'log_max_files' => 30
When the user is using Monolog, there may be different levels of severity of the log messages that are produced by the application. All logs are written to storage by Laravel. But, in the production environment, the user is free to configure the minimum severity that the user wishes to log by adding the log_level option to the app.php configuration file.
'log_level' => env ('APP_LOG_LEVEL', 'error'),
How does Laravel Error Works?
Laravel ships along with its error reporting set to E_ALL. It includes the Whoops framework. It helps to create readable stack traces that assist in finding out the exact line from where the PHP error occurred.
Laravel redirects all of the requests it receives to a particular central index.php file.
#File: index.php
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/start.php';
$app->run();
There is a particular line that helps in pulling in the application bootstrap termed as start.php file. It also calls the run method in the application. The start file in the Bootstrap is the place from where the Laravel core represents as the application file. It later returns the object of the application from that place.
#File: bootstrap/start.php
$app = new Illuminate\Foundation\Application;
//...
return $app;
You can also return a value from a PHP include or require statement.
#File: bootstrap/start.php
$framework = $app['path.base'].
'/vendor/laravel/framework/src';
require $framework.'/Illuminate/Foundation/start.php';
The bootstrap/start.php file is present inside the root of the project folder of the application. When the user makes the primary and first start.php file perform a (require)action on the latter or the second start.php file, it ascertains that the application manager can benefit as the user can easily work on whatever future improvements may take place to the Laravel framework.
The startExceptionhandling method is where the Laravel framework sets up its custom error handlers.
#File: vendor/laravel/framework/src/Illuminate/Foundation/start.php
$app->startExceptionHandling();
if ($env != 'testing') ini_set('display_errors', 'Off');
The second line is as follows:
#File: vendor/laravel/framework/src/Illuminate/Foundation/start.php
if ($env != 'testing') ini_set('display_errors', 'Off');
Example of Laravel Error
Using the controller we will help in finding the error in Laravel. If we have data that has user information and we need to look for errors in the data handling by Laravel, we need to follow these steps.
$user = User::find ($request->input ('user_id'));
This is the picture we will find when the user is not found.
We can now set .env file with APP_DEBUG= false. Using User::findORFail() we can find the error too. If Laravel does not find the user, it will show a 404 page with the mention as the page you are looking for cannot be found.
After this, we can catch the errors in the framework with regards to the data we need to crosscheck and redirect them back to the form where the user details are present with the actual understandable message of error in detail.
It is important for the user to know the exception type before redirecting the details. In the situation of User::findORFail(), it will show an Eloquent exception Modelnotfoundexception. We will have to, therefore, add the following codes.
Public function search(Request $request)
{
try {
$user = User::findOrFail($request->input('user_id'));
} catch (ModelNotFoundException $exception) {
Return back()->withError($exception->getMessage())->withInput();
}
return view('users.search', compact('user'));
}
When you wish to show an error in blade:
<h3 class="page-title text-center">Search for user by ID</h3>
@if (session('error'))
<div class="alert alert-danger">{{ session('error') }}</div>
@endif
<form action="{{ route('users.search') }}" method="POST">...</form>
To get our own message add:
return back()->withError('User not found by ID ' . $request->input('user_id'))->withInput();
Output:
Conclusion
In conclusion, we can say that the Laravel Error works efficiently to resolve all the problems that may be logged as errors in the log system files of Laravel. There are ways and means to identify them and detect the errors to rectify them and resolve the persisting problems. If the proper procedures as mentioned above are followed, the errors will be handled efficiently.
Recommended Articles
We hope that this EDUCBA information on “Laravel Errors” was beneficial to you. You can view EDUCBA’s recommended articles for more information.