We're happy to introduce our latest error reporting SDK, for Laravel, which tracks and reports errors that happen in your Laravel applications. Laravel is a popular and powerful MVC framework for PHP, designed for developers who need a simple and elegant toolkit to create full-featured PHP web applications.
In real-world production applications, it’s important to monitor errors so you understand your user’s experience and can fix issues before more are impacted. Rollbar helps by providing you with a live error feed from your application, including complete stack traces for instant visibility. To give you more context to debug problems, we also track the environment the error is coming from (prod or staging), the server that generated the error, and even the user’s session. Learn more about Rollbar’s product features.
Below, we'll show you how to add Rollbar error reporting to your Laravel apps and even give you a code example that you can try yourself.
Laravel error reporting with Rollbar
Here are some simple steps describing how to integrate Laravel SDK in your code. You can find more details in the Laravel Documentation.
- Sign up for a Rollbar account if you haven’t done so yet.
- Create your project and select Other from the list of notifiers. Copy the server side access token that is generated for you. You’ll need this to configure Rollbar in the steps below.
-
Open command prompt in your project directory and type following command to install the Laravel notifier SDK.
composer require rollbar/rollbar-laravel
-
Add the project access token from step 1 in your .env file. You can find .env file in your project directory.
ROLLBAR_TOKEN=[your Rollbar project access token]
-
Add the Rollbar service provider in the providers array
config/app.php
. You can find the config directory under your project.Rollbar\Laravel\RollbarServiceProvider::class,
-
If you only want to enable Rollbar reporting for certain environments you can conditionally load the service provider in your
AppServiceProvider
. You can find it in theapp/providers
folder under project directory.public function register() { if ($this->app->environment('production')) { $this->app->register(\Rollbar\Laravel\RollbarServiceProvider::class); } }
-
Add the following snippet in the
config/services.php
. You can find the config directory under the main project directory.'rollbar' => [ 'access_token' => env('ROLLBAR_TOKEN'), 'level' => env('ROLLBAR_LEVEL'), ],
The level variable defines the minimum log level at which log messages are sent to Rollbar. If not specified, the default is debug. For development, you could set this either to debug to send all log messages, or to none to send no messages at all. For production, you could set this to error or warn so that info and debug messages are ignored.
Testing Rollbar with an example Laravel application
To test that it’s working, let’s create a page that will generate an error message. In the example below, you will be able to generate an error by clicking the “Create an error” button.
[
{:target="_blank"}
{: .imgcaption}
Laravel example application
Check out our project on GitHub called Rollbar-Laravel-Example{:target="_blank"}. To run it, clone the project and replace the access token with your project access token. You can find the access token inside .env file under your project directory.
When you click the “Create an error” button it will generate an error message. It triggers the code>MessagesController@submit action which has mapped to the home/submit url. In the submit method of MessagesController
, we attempt to access the foo field on x, which is empty. This results in the error “ErrorException: Create default object from empty value”.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MessagesController extends Controller
{
public function submit(){
try{
$x = null;
$x->foo = 5; // Null field access
}catch(Exception $ex){
echo "Caught exception";
}
return "SUCCESS";
}
}
Viewing and reporting Laravel errors in Rollbar
Next open Rollbar to see what these Laravel errors look like in your account’s error occurrences page. The error we just generated should be called “ErrorException: Create default object from empty value”.
{: .imgcaption}
Laravel errors as, seen in Rollbar
Get more details by clicking on the error. You can now see a traceback showing you the exact source code file, method, and line number that generated the error.
{: .imgcaption}
Laravel error stack trace as seen in Rollbar
For errors that impact many customers or are difficult to troubleshoot, Rollbar gives you additional context to solve them faster. With source code integration, you can click on the file to view in GitHub and see what changes were recently made. You can also see when the problem started, which deployment is suspected, and even which browsers and people are affected. I can also set alerts when the problem happens again. This will help you quickly determine the business impact, fix the problem, and proactively respond in the future.
If you haven’t already, sign up for a 14-day free trial of Rollbar and let us help you take control of Laravel errors.