Laravel debugging Part 1 – Laravel Telescope

In this series we will discuss about many tools to debug in laravel. Today we will talk about debugging using Laravel Telescope the official package from the creator of laravel.

What is Laravel Telescope ?

Laravel Telescope is an elegant debug assistant for the Laravel framework. Telescope provides insight into the requests coming into your application, exceptions, log entries, database queries, queued jobs, mail, notifications, cache operations, scheduled tasks, variable dumps and more. Telescope makes a wonderful companion to your local Laravel development environment.

Prerequisites

Before installing Laravel Telescope you need to have a working installation of laravel. Specifically you need and installation >= 5.7.7 . If your installation is lower you may upgrade by following the official guide. In case you want a fresh installation you can read our article on laravel installation.

Installation

To install Laravel Telescope you simple need to run this command in the root directory of your project :

composer require laravel/telescope

In case you do not have the bcmath extension installed, you might get this error :

Problem 1
    - moontoast/math 1.1.2 requires ext-bcmath * -> the requested PHP extension bcmath is missing from your system.
    - moontoast/math 1.1.1 requires ext-bcmath * -> the requested PHP extension bcmath is missing from your system.
    - moontoast/math 1.1.0 requires ext-bcmath * -> the requested PHP extension bcmath is missing from your system.
    - laravel/telescope v0.1.5 requires moontoast/math ^1.1 -> satisfiable by moontoast/math[1.1.0, 1.1.1, 1.1.2].
    - Installation request for laravel/telescope ^0.1.5 -> satisfiable by laravel/telescope[v0.1.5].

If that’s happen simply install the required extension if you are on Linux or enable it if you are using windows.

sudo apt install php-bcmath
sudo service apache2 restart

Now you can run the installation again by using composer require laravel/telescope.

After installation complete now publish the assets and run database migration with those command.

php artisan telescope:install
 
php artisan migrate

Install only on local

If you plan to do debugging only on local development you may install it on for local.

composer require laravel/telescope --dev

Configuration

When you finish your installation you can access the Telescope dashboard by following the route /telescope in your browser.

Laravel telescope home page
Laravel Telescope home

You can change this route in the config/telescope.php file. This file allows you to configure Telescope. Let’s take a look at the most important one.

path

   /*
    |--------------------------------------------------------------------------
    | Telescope Path
    |--------------------------------------------------------------------------
    |
    | This is the URI path where Telescope will be accessible from. Feel free
    | to change this path to anything you like. Note that the URI will not
    | affect the paths of its internal API that aren't exposed to users.
    |
    */
 
      'path' => env('TELESCOPE_PATH', 'telescope'),

As we can see in the comment, this is the path in your app in which Telescope will be accessible. Don’t include slashes.

driver

    /*
    |--------------------------------------------------------------------------
    | Telescope Storage Driver
    |--------------------------------------------------------------------------
    |
    | This configuration options determines the storage driver that will
    | be used to store Telescope's data. In addition, you may set any
    | custom options as needed by the particular driver you choose.
    |
    */
 
     'driver' => env('TELESCOPE_DRIVER', 'database'),
 
    'storage' => [
        'database' => [
            'connection' => env('DB_CONNECTION', 'mysql'),
            'chunk' => 1000,
        ],
    ],

The driver parameter define where Telescope gonna store the data. The default value is database. You can change the database if you do not want to put the telescope data in the same database of your application.

Enabled

  'enabled' => env('TELESCOPE_ENABLED', true),

It’s just a convenient way in case you wanna disable Laravel Telescope.

Access and restriction

In your local environment, you may let any user access Telescope. In other environments, you may let only selected users access Telescope. To configure the access in, let’s say, production, you need to go to app/Providers/TelescopeServiceProvider.php to the gate function.

 /**
     * Register the Telescope gate.
     *
     * This gate determines who can access Telescope in non-local environments.
     *
     * @return void
     */
    protected function gate()
    {
        Gate::define('viewTelescope', function ($user) {
            return in_array($user->email, [
                //
            ]);
        });
    }

Here you can add a list of email you want to have access to Telescope data. According to me it’s not the a very good way to do things for many reason. First you gonna expose email in the code and if you are developing an opensource project. If you host it on github you maybe you will forget to remove them.

Secondly maybe mail you use in development is not the same you gonna use in a production environment. A better way to do it could be using environment variable.

 /**
     * Register the Telescope gate.
     *
     * This gate determines who can access Telescope in non-local environments.
     *
     * @return void
     */
    protected function gate()
    {
        Gate::define('viewTelescope', function ($user) {
            $ids = env('TELESCOPE_IDS', '');
            $ids = explode(',', $ids);
            return in_array($user->id, $ids);
        });
    }

Then set the environment variable in your .env file and .env.example also in case your are not the only developer.

TELESCOPE_IDS=1,2

Conclusion

Thanks you for reading this article about Laravel debugging using Telescope package. We will continue this series with other third party package. If you get any trouble while installing it you may leave a comment i will try to help you.

Now let’s explore the Laravel Telescope

Laravel Telescope request details

Let’s take a look a the request details.

I hope this tools let’s you debug your application easily

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *