Laravel integration guide
Typical installation time: 5 minutes
Hi there! You’ve found Honeybadger’s guide to Laravel error and exception tracking. Once installed, Honeybadger will automatically report errors wherever they may happen:
- During a web request
- In a scheduled command
- In a background task
- When a process crashes
Installation
Section titled “Installation”First, install the honeybadger-laravel package via composer:
composer require honeybadger-io/honeybadger-laravelLaravel version support
Section titled “Laravel version support”Install the version of our package based on the version of Laravel you are using:
| Laravel Version | Honeybadger Laravel Version |
|---|---|
| 12.x | 4.x (current) |
| 11.x | 4.x (current) |
| 10.x | 4.x (current) |
| 9.x | 3.13.x |
| 8.x | 3.2.x |
| 7.x | 3.x |
| 6.x | 2.x |
| 5.x | 1.x |
Next, add Honeybadger reporting to Laravel 11 and later by adding the following
to bootstrap/app.php
(https://laravel.com/docs/12.x/errors#reporting-exceptions):
->withExceptions(function (Exceptions $exceptions) { $exceptions->report(static function (Throwable $e) { if (app()->bound('honeybadger')) { app('honeybadger')->notify($e, app('request')); } });})In Laravel 8.5 to 10, the default handler comes with a
register() method;
you should add the Honeybadger client within the reportable() callback.
public function register(){ $this->reportable(function (Throwable $e) { if (app()->bound('honeybadger')) { app('honeybadger')->notify($e, app('request')); } });}On earlier versions of Laravel, add the client within the report($exception)
method:
public function report(Throwable $exception){ if (app()->bound('honeybadger') && $this->shouldReport($exception)) { app('honeybadger')->notify($exception, app('request')); } parent::report($exception);}Finally, run the honeybadger:install artisan command.
php artisan honeybadger:install [Your project API key]If you are using our EU stack, add the --endpoint and the --appEndpoint
flags to the honeybadger:install command:
php artisan honeybadger:install [Your project API key] --endpoint=https://eu-api.honeybadger.io --appEndpoint=https://eu-app.honeybadger.ioThe honeybadger:install command does three things:
- Adds
HONEYBADGER_API_KEYto.envand.env.example - If you added the
--endpointand--appEndpointflags, it also addsHONEBADGER_ENDPONTandHONEYBADGER_APP_ENDPOINTto.envand.env.example. - Creates Honeybadger’s
config/honeybadger.phpconfiguration file - Sends a test notification to your Honeybadger project
If everything is set up correctly, you should now have an error report in Honeybadger!
Identifying users and controller/action
Section titled “Identifying users and controller/action”Honeybadger automatically captures details about the current logged-in user, as well as the controller and method name. No extra configuration needed. We only capture the user ID, so no sensitive information is transmitted.
When an error occurs, you’ll see an Affected Users section on your dashboard, where we’ll list the user IDs and how many times they encountered the error.
Adding context
Section titled “Adding context”Context can be added by either the provided Facade or by resolving from the service container.
Facade
Section titled “Facade”Honeybadger::context('key', $value);DI resolution
Section titled “DI resolution”use Honeybadger\Honeybadger;
public function __construct(Honeybadger $honeybadger){ $honeybadger->context('key', $value);}Helper resolution
Section titled “Helper resolution”use Honeybadger\Honeybadger;
public function __construct(){ app('honeybadger')->context('key', $value); app(Honeybadger::class)->context('key', $value)}Check-Ins
Section titled “Check-Ins”honeybadger-laravel integrates with
Honeybadger’s Check-In feature to help
you know when your scheduled tasks and background jobs go missing or silently
fail.
To get started, create a new check-in in the Check-Ins tab of your project dashboard. You’ll be given a check-in URL that looks like this: https://api.honeybadger.io/v1/check_in/Jiy63Xw. Take note of the check-in ID; it’s the last part of the check-in URL. In this example, it’s Jiy63Xw. Alternatively, you can setup your check-ins entirely within your configuration file. If you follow this method, you don’t need the check-in ID anymore and instead you can use the check-in slug.
Run a one-off check-in
Section titled “Run a one-off check-in”To run a one-off check-in, use the honeybadger:checkin command with your
check-in ID. This will let Honeybadger know that your app is alive.
php artisan honeybadger:checkin Jiy63XwOr if you have configured your check-ins in your configuration file:
php artisan honeybadger:checkin "my-checkin"Scheduled command
Section titled “Scheduled command”You can schedule the check-in command to run at an interval. This method is great for ensuring your application is up and running.
protected function schedule(Schedule $schedule){ $schedule->command('honeybadger:checkin Jiy63Xw')->everyFiveMinutes(); // or using the check-in slug $schedule->command('honeybadger:checkin "my-checkin"')->everyFiveMinutes();}After a scheduled command
Section titled “After a scheduled command”You can use the thenPingHoneybadger($checkInId) macro to check-in after
certain scheduled commands are run. This method is great for making sure
specific scheduled commands are running on time.
protected function schedule(Schedule $schedule){ $schedule->command(SendEmails::class)->daily() ->thenPingHoneybadger('Jiy63Xw'); // or using the check-in slug ->thenPingHoneybadger('my-checkin');}In this example, if SendEmails fails to run for some reason, Honeybadger will
notify you.
You can also specify the environments where the check-in is allowed to run:
protected function schedule(Schedule $schedule){ $schedule->command(SendEmails::class)->daily() ->thenPingHoneybadger('Jiy63Xw', 'production'); // or using the check-in slug ->thenPingHoneybadger('my-checkin', 'production'); $schedule->command(CheckStatus::class)->daily() ->thenPingHoneybadger('Jiy63Xw', ['production', 'staging']); // or using the check-in slug ->thenPingHoneybadger('my-checkin', ['production', 'staging']);}After a successful scheduled command
Section titled “After a successful scheduled command”You can use the pingHoneybadgerOnSuccess($checkInId) macro to ensure that a
certain command was run and completed successfully. This method is great for
making sure specific scheduled commands are running on time only if it was
successful. Like the thenPingHoneybadger method, you can also restrict it to
specific environments.
protected function schedule(Schedule $schedule){ $schedule->command(SendEmails::class)->daily() ->pingHoneybadgerOnSuccess('Jiy63Xw', 'production'); // or using the check-in slug ->pingHoneybadgerOnSuccess('my-checkin', 'production');}Using Honeybadger as a logger
Section titled “Using Honeybadger as a logger”If you prefer, you can also use Honeybadger as a log channel in your Laravel
app. To do this, you’ll need to
register a custom channel
in your config/logging.php, making use of the HoneybadgerLogDriver:
'channels' => [ // ... 'honeybadger' => [ 'driver' => 'custom', 'via' => Honeybadger\HoneybadgerLaravel\HoneybadgerLogDriver::class, 'name' => 'honeybadger', 'level' => 'error', ], ],Now you can write log messages as normal with Laravel’s log facade, and they’ll show up on your Honeybadger dashboard.
Log::channel('honeybadger')->error('An error message');Log::channel('honeybadger')->error('An error message with context', ["some-key" => "some-value"]);Log::channel('honeybadger')->error($exception);If you include an exception context item in your error messages, we’ll
automatically format them for easy viewing:
$e = new \Exception('Something happened');Log::channel('honeybadger')->error('An error message', ['exception' => $e]);You can also add the custom channel to your default stack so you can automatically have exceptions logged to Honeybadger as well:
'channels' => [ 'stack' => [ 'driver' => 'stack', 'channels' => ['single', 'honeybadger'], 'ignore_exceptions' => false, ], // ... ],