Replacing Dump & Die with Ray when Debugging
Tags: debugging, laravel, php, testing
Published: 2021-01-21 02:36:41


*This post is not sponsored and covers my experience with using Ray.*

[Ray](https://myray.app/) is a tool released by the wicked team [Spatie](https://spatie.be/). Ray aims to replace the dump & die method of debugging by using an interface to catch all the debug output. There are currently adaptors for:

- Laravel,
- WordPress 
- Yii2
- Craft CMS
- JavaScript
- and more

## Why Ray?

Since I started coding, Dump & Die has been the way I have debugged applications. I never enjoyed using xdebug as I found it clunky and a pain to set up - I hear this is less of a problem now.

When Spatie announced the release of Ray I wasn't completely sure what to think. Ray felt like it was just a UI that wrapped my debug statements and brought me out the browser. 

Since they have a limited trial available I thought I would give it a spin anyway and see if it could help. After a few days I was happy to buy Ray and found it helped me out for a few reasons:

- A history of debug statements. No more forgetting the last thing you dumped and having to redo it again. Statements can be grouped into different pages to make finding them easier.
- When running tests you can send the output to ray. The same goes for console commands, jobs, events, and - well - any code in your application. No running through the terminal to find the output!
- When calling `ray($someData)` it will work out the best way to interpret the data. Whether that is a model, image, string, or anything else. If you want the full log you can get that by using the `ray->raw($someData)` method.
- Handy shortcuts to bring ray in and out of view - making debugging quick.
- A lot of services integrate with ray (and it is growing every week). This makes it useful for collating debugging statements into one interface.
- You can install ray to your production dependencies. When code is in a production environment any `ray()` statements will silently fail instead of displaying to the user.

## How to use Ray

### Showing Queries

To get all queries executed between lines of code you can use the `showQueries` method which begins tracking them.

```php
ray()->showQueries();

User::find(3);

ray()->stopShowingQueries();
```

![ray-query](https://dev-to-uploads.s3.amazonaws.com/i/jd9gl70txeqv93zq7q0j.jpg)


### Capturing Models

The `model()` method prints out an array of the model's data to `ray`. You can also get the full output by using the `raw()` command.

```php
ray()->model($user); // Shows an array of values.
```

![ray-model-pretty](https://dev-to-uploads.s3.amazonaws.com/i/2g8r8egs89qy3oimckp3.jpg)
 

```php
ray()->raw($user); // prints out the full output of $user.
```

![ray-model-raw](https://dev-to-uploads.s3.amazonaws.com/i/3dditi6nzl9dq4ri6f7m.jpg)
 
### Viewing Mailables

To see the rendered version of a mailable you can use `mailables()`.

```php
ray()->mailable(new TestMailable());
```

![ray-mailable](https://dev-to-uploads.s3.amazonaws.com/i/vgh1uv3ckzqlqqzlp57i.jpg) 

### Outputting Images

To check an image you can use the `image()` method to have it displayed in ray.

```php
ray()->image('https://picsum.photos/200');
```

![ray-image](https://dev-to-uploads.s3.amazonaws.com/i/gsvkt5gpaqus2v2nnlsk.jpg) 

## Conclusion

There are a lot of handy methods included within ray so far and more are being added as the team release updates. If you've been hesitating giving ray a try I'd highly recommend downloading the trial and giving it a play.