Laravel Telescope is a powerful debugging and introspection tool for Laravel applications. It allows you to monitor various aspects of your application, including SQL queries. Here's a step-by-step guide on how to use Laravel Telescope to monitor and profile SQL queries:
You can install Laravel Telescope using Composer. Open your terminal and run the following command:
bashcomposer require laravel/telescope
Then, run the Telescope installation command:
bashphp artisan telescope:install
Finally, run the migrations to create the necessary database tables:
bashphp artisan migrate
By default, Telescope is only enabled in the local environment. You can customize this in the TelescopeServiceProvider
class. Open App\Providers\TelescopeServiceProvider.php
and modify the register
method:
phppublic function register()
{
Telescope::ignoreMigrations();
Telescope::filter(function (IncomingEntry $entry) {
// Additional filters if needed
return true;
});
}
You can access the Telescope dashboard by visiting /telescope
in your web browser. If you're running your Laravel application locally, the URL would be something like http://localhost:8000/telescope
.
Telescope automatically records SQL queries and displays them in the dashboard. You can find them under the "Queries" tab. It provides details like the query, the time it took to execute, and any bindings used.
Telescope Documentation:
Video Tutorial:
Blog Posts:
GitHub Repository:
Let's say you have a controller method that fetches users from the database. Here's an example:
phpuse App\Models\User;
public function index()
{
$users = User::all();
return view('users.index', compact('users'));
}
When you access this route, Laravel Telescope will record the SQL queries involved in fetching the users, and you can view them in the Telescope dashboard.
Remember to remove Telescope from your production environment, as it's intended for development and debugging purposes.
I hope this helps you get started with Laravel Telescope for monitoring and profiling SQL queries!
=== Happy Coding :)