How to Enable Debug Mode in Laravel

How to Enable Debug Mode in Laravel

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.

  1. Open your Laravel project in your file explorer or IDE (like Visual Studio Code, PhpStorm, etc.).

  2. In the root directory of the project, you will find the .env file.

  3. 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:

APP_DEBUG=false

Change the value from false to true:

APP_DEBUG=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:

APP_ENV=local

So your .env file should look something like this:

APP_ENV=local APP_DEBUG=true APP_KEY=base64:your-app-key-here DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_db DB_USERNAME=root DB_PASSWORD= MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=your-email@example.com MAIL_PASSWORD=your-email-password MAIL_ENCRYPTION=tls

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:

php artisan config:clear

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.

  1. Go to the resources/views/errors/ directory.

  2. 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:

<!-- resources/views/errors/404.blade.php --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Page Not Found</title> </head> <body> <h1>Oops! The page you're looking for cannot be found.</h1> <p>We couldn't find the page you were looking for. Try going back to the homepage.</p> </body> </html>

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.

  1. Open your .env file again.

  2. Change APP_DEBUG=true back to false.

APP_DEBUG=false APP_ENV=production

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

  1. 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.

  2. 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:

  1. Set APP_DEBUG=true in the .env file.

  2. Set APP_ENV=local for local development.

  3. Clear the cache with php artisan config:clear.

  4. Make sure to disable debug mode in production by setting APP_DEBUG=false.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close