Dumping in Method Chains0:00
Alright, enough about the Laravel 11 skeleton, let's talk about some new features that Laravel makes available to you. For a long time now we've had the dump and dumpAndDie functions available to us in Laravel. If I use dump, let's say Hello World, and then we visit the front end, you can see Hello World dumped at the top of the screen here. If I do dumpAndDie, well it's going to finish the execution of the script early and we'll just see Hello World output on the front end. Recently we've seen a number of classes appear in Laravel with dump and dumpAndDie methods that allow you to perform the same action inside a chain. An example of this is Eloquent models.
that allow you to perform the same action inside a chain. An example of this is Eloquent models. So I could say, give me users with their notifications, maybe I want them in latest order, and let's limit to the top 5 and then call get. If I want to inspect the query before we retrieve the results, well I can go ahead and use the dumpAndDie method that is available on the Eloquent model. And on the front end, well we can now inspect that query to make sure that it's correct. Likewise if we don't want to stop script execution, we have the dump method which will still go ahead and perform the query, but will dump out the query to the front end as well for us to inspect and take a look at.
Stringable Dump Example1:16
ahead and perform the query, but will dump out the query to the front end as well for us to inspect and take a look at. Another example of this in Laravel is the Stringable class. So if I use the string helper, we'll say hello, say I want to append world, and I want to use ucwords capitalization, and then we'll call toString at the end. Again anywhere in this method chain I'm able to string on dump and die and then see that output either in the front end or in the console, whatever execution environment I'm currently using. I can also use dump if I don't want to stop execution. And if I place this here, we see hello world with a lowercase w, but if I was to place
Introducing Dumpable Trait1:50
I can also use dump if I don't want to stop execution. And if I place this here, we see hello world with a lowercase w, but if I was to place this here instead, then I would imagine, yeah, we have an uppercase w because it's been converted to APA case. So what exactly is new here? Well previously the dump and dumpAndDie methods on these various classes were all implemented differently and separately, but they've now been consolidated into a single trait called Dumpable. As you'd expect, Dumpable has two methods, dumpAndDie and dump, as you can see here. So what's the big deal?
As you'd expect, Dumpable has two methods, dumpAndDie and dump, as you can see here. So what's the big deal? Yeah, it's cleaned things up in the Laravel code base, but by providing this as a trait, well we can add it to our own classes if we want similar functionality. To illustrate this, I have gone ahead and stolen the Assistant class that Jeffrey's been building as part of his OpenAI exploration. And as you can see, I'm sending a system message and a couple of questions to OpenAI. In between those questions, OpenAI is responding. So we're building up an array of messages. There is no dumpAndDie or dump available on this class yet, but I can easily add it thanks
Adding Dumpable to Class2:55
So we're building up an array of messages. There is no dumpAndDie or dump available on this class yet, but I can easily add it thanks to that new Dumpable trait. So use Dumpable. And all of a sudden, anywhere in this chain, I can dump or dumpAndDie out the messages at that point in time. If we go to the front end now, we can see an array. And in that array, we have the messages for the questions that we've asked so far. If I go ahead and add dumpAndDie to the end here instead, well now we're going to see extra messages, an array with five entries because OpenAI has had another question and
Overriding Dump Output3:23
If I go ahead and add DumpAndDie to the end here instead, well now we're going to see extra messages, an array with five entries because OpenAI has had another question and had time to respond as well. Of course, you're free to edit the output by overriding the Dump method. So perhaps here we could come ahead and override Dump. By default, Dump, if you look at the underlying code, is basically just going to dump this passing any arguments you've provided, but you can change that completely. For example, let's say when we dump or DumpAndDie, we only want to display messages that come from OpenAI rather than displaying questions that we've asked. So we could collect up this messages and let's filter down.
from OpenAI rather than displaying questions that we've asked. So we could collect up this messages and let's filter down. Of course, we can filter based on the message role. So we'll say only return this if the current role is equal to assistant. Now we can wrap this in a call to dump, return this at the end of this class so that the chain still works and everything else should work as expected. Now instead of seeing the full output of the class, we're just seeing the two items that we filtered down to, in other words, the assistance responses. And even though we overrode the Dump method, not the DumpAndDie method, we still see the filtered output inside DumpAndDie because the Dumpable trait uses dump under the hood.
Wrap-Up and Use Cases4:35
And even though we overrode the dump method, not the dumpAndDie method, we still see the filtered output inside dumpAndDie because the Dumpable trait uses dump under the hood for both, which is super handy. So there we are. That's the new Dumpable trait, and I think it's great for any class that has chained method calls where it would be annoying to have to custom wrap a certain portion of that chain using the dump or dumpAndDie functions. Just drop the concern on your class, and you're off to the races.
