spatie/laravel-ray
Send Laravel debug output to Ray, Spatie’s desktop debugging app. Use a consistent API to inspect variables, arrays, HTML, queries and more, measure performance, and pause execution. Works across Laravel/PHP with Ray’s rich UI.
Installation:
composer require spatie/laravel-ray
php artisan ray:install
This publishes the config file (config/ray.php) and registers the service provider.
Configure Ray:
Edit config/ray.php to set your project name, token, and other settings. Ensure the token matches your Ray app configuration.
First Use Case:
Replace a dd() or dump() call in your code with:
ray('Variable to inspect', ['context' => 'Debugging user creation']);
Launch the Ray app, and the output will appear in real-time.
@ray, @raydump, @xray) for debugging views.php artisan ray:clean to remove Ray calls from your codebase.config/ray.php under watchers).Debugging Variables:
ray($user, ['context' => 'User profile data']);
ray(['status' => 'success', 'data' => $data]);
Blade Debugging:
@ray($user)
@xray <!-- Shows all variables in scope -->
Performance Measurement:
ray()->measure('Database query', function () {
User::all();
});
Exception Context:
try {
// Risky code
} catch (\Exception $e) {
ray($e, ['context' => 'Failed payment processing']);
throw $e;
}
Conditional Debugging:
Use config('ray.enabled') to toggle Ray output in different environments:
if (config('ray.enabled')) {
ray($data, ['context' => 'Debug only in local']);
}
Custom Watchers:
Extend the Spatie\Ray\Watchers\Watcher class to create domain-specific watchers (e.g., for API requests or third-party services).
Queue Jobs:
Use ray() in job handlers to inspect payloads or failures:
public function handle() {
ray($this->data, ['context' => 'Processed job']);
}
API Responses: Log API responses with context:
$response = Http::get('https://api.example.com/data');
ray($response->json(), ['context' => 'API response']);
Performance Overhead:
if (app()->environment('local')) {
ray($data);
}
Token Leaks:
config/ray.php with a token. Use environment variables:
RAY_TOKEN=your_token_here
Then reference it in ray.php:
'token' => env('RAY_TOKEN'),
Blade Directives in Production:
'enabled' => false in config/ray.php or via .env:
RAY_ENABLED=false
Query Watcher Conflicts:
QueryWatcher is enabled in config/ray.php:
'watchers' => [
'queries' => true,
],
PHP 8.5+ Deprecations:
strtolower on null). Ensure you’re on >=1.41.0.Missing Output:
config/ray.php for misconfigured token or project_name.Spatie\Ray\RayServiceProvider is registered in config/app.php.Artisan Command Issues:
ray:clean fails, manually remove Ray calls or check for syntax errors in your codebase.Context Overload:
ray($user, ['context' => 'User ID: ' . $user->id]);
Blade @xray Not Working:
BladeWatcher is enabled:
'watchers' => [
'blade' => true,
],
Custom Directives:
Create a custom directive by extending Spatie\Ray\Directives\Directive and binding it in the service provider.
Middleware Integration: Use Ray in middleware to log incoming requests:
public function handle($request, Closure $next) {
ray($request->all(), ['context' => 'Incoming request']);
return $next($request);
}
Event Listeners: Log events with context:
public function handle(UserRegistered $event) {
ray($event->user, ['context' => 'New user registered']);
}
Testing:
Mock Ray calls in tests using Spatie\Ray\Facades\Ray:
Ray::shouldReceive('ray')->once();
How can I help you explore Laravel packages today?