Replacing Dump & Die with Ray when Debugging

jackabox

Jack Whiting

Posted on January 21, 2021

Replacing Dump & Die with Ray when Debugging

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

Ray is a tool released by the wicked team Spatie. 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.

ray()->showQueries();

User::find(3);

ray()->stopShowingQueries();
Enter fullscreen mode Exit fullscreen mode

ray-query

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.

ray()->model($user); // Shows an array of values.
Enter fullscreen mode Exit fullscreen mode

ray-model-pretty

ray()->raw($user); // prints out the full output of $user.
Enter fullscreen mode Exit fullscreen mode

ray-model-raw

Viewing Mailables

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

ray()->mailable(new TestMailable());
Enter fullscreen mode Exit fullscreen mode

ray-mailable

Outputting Images

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

ray()->image('https://picsum.photos/200');
Enter fullscreen mode Exit fullscreen mode

ray-image

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.

💖 💪 🙅 🚩
jackabox
Jack Whiting

Posted on January 21, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related