Step 1: Locate the .env
File
The .env
file is located in the root directory of your Laravel project. This file is used to store environment-specific configurations such as database credentials, mail settings, and debug settings.
-
Open your Laravel project in your file explorer or IDE (like Visual Studio Code, PhpStorm, etc.).
-
In the root directory of the project, you will find the
.env
file. -
Open this file for editing.
Step 2: Change the Debug Setting in .env
File
Inside the .env
file, find the line that looks like this:
Change the value from false
to true
:
This will enable debug mode, which will allow detailed error messages to be shown in your browser.
Additionally, check the APP_ENV
setting to ensure that it's set to local
, which is typically used in a development environment:
So your .env
file should look something like this:
Step 3: Clear the Cache (if needed)
Laravel caches the configuration to optimize performance. After editing the .env
file, the changes might not take effect immediately due to this cached configuration.
To clear the cache and make sure that the new debug setting takes effect, run the following Artisan command in your terminal:
This will clear the cached configuration files, allowing the new settings from the .env
file to be applied.
Step 4: View Debug Information
Once you’ve enabled debug mode, Laravel will show detailed error information in your browser when an error occurs. For example:
-
Detailed Error Pages: If an error occurs, Laravel will display a detailed error page with the file, line number, and stack trace where the error occurred. This helps you pinpoint the exact location of the problem in your code.
-
Database Queries: If the error is related to database queries, you’ll be able to see the raw SQL queries executed, helping you debug issues related to your database.
Step 5: Handling Errors
Laravel has a built-in exception handler that is responsible for showing error messages. With debug mode enabled, Laravel will display:
-
The stack trace showing where the error occurred.
-
The error message that gives details about the issue.
-
The line number and file path where the error occurred, making it easier to find the bug.
Step 6: Customize Error Pages (Optional)
If you want to customize how Laravel displays errors, you can modify the error pages by creating views for specific errors like 404
(Page Not Found), 500
(Server Error), etc.
-
Go to the
resources/views/errors/
directory. -
Create custom error views, such as
404.blade.php
,500.blade.php
, or other error types you want to handle.
For example, to create a custom 404 error page, create a 404.blade.php
file in the resources/views/errors/
folder:
Step 7: Turn Off Debug Mode in Production
Debug mode should always be disabled in production for security reasons. You don’t want to expose sensitive information like file paths, stack traces, or database queries to the end-users.
-
Open your
.env
file again. -
Change
APP_DEBUG=true
back tofalse
.
This will ensure that your application doesn't display sensitive error messages in the browser when it’s deployed in a live environment.
Step 8: Verify Debug Mode
-
In Local Development:
-
Visit your Laravel application in the browser.
-
Trigger an error (e.g., by visiting a non-existing route or causing a syntax error).
-
You should see a detailed error page with the error message, file, and line number, along with a stack trace.
-
-
In Production:
-
After setting
APP_DEBUG=false
, if you trigger an error, the user will see a general error page like "Whoops, looks like something went wrong" without detailed information.
-
Conclusion
By enabling debug mode in Laravel during development, you gain detailed error messages, stack traces, and other helpful debugging information. Just follow these steps:
-
Set
APP_DEBUG=true
in the.env
file. -
Set
APP_ENV=local
for local development. -
Clear the cache with
php artisan config:clear
. -
Make sure to disable debug mode in production by setting
APP_DEBUG=false
.