Laravel tip: Higher order collection proxies (and Zevia)
Poppin off is a series of short code tips, tricks, patterns, and refactors for software developers. Oh and also seltzer reviews. Higher order collection proxies are one of Laravel's neatest bits of magic. Here is a way to use them that you might not expect!
We also do a review of Zevia's Cream Soda. Send me a seltzer or hipster soda!
Everything I said
Here we go. Today on Poppin' Off, we're drinking a Zevia Cream Soda, and we're gonna talk about Laravel's higher order proxies. Now, first things first, Zevia, not that good.
You get used to it, but it's not that good. The cream soda may be the best. Um, but it's not that good. Kinda makes your stomach hurt. Laravel's higher order proxies, on the other hand, are pretty good. Let me show you. I've got a desk test right here, which is a Laravel browser automation. I just made a video about it. It's a very good video. You should go watch. But one thing that I don't like is, look, we're opening two different browsers, and then we're doing the same thing in each browser to prepare it, to get it set up, and then we do some stuff
on browser one only and some stuff on browser two only. So if we were to move this around a little bit, let's move this around. We'll move this guy down. We'll move this guy up. And now, and it's, it's just painfully obvious, those are the same. Those are the same. Those are the same. Those are the same. These are different. So what we're gonna do, what if we did this?
What if we collected browser one and browser two, and we mapped that to both? And if we did DD of both, now let's just run over here, and it's gonna launch, boom, two browsers. But if we check back here, okay, it looks like we have our collection, which is pretty cool.
So collection items two. That is correct. Now, what if we just hung... This is weird. What if we just hung an each off of it? Not, not, not an each callback. Uh, we just not, we're not doing that. Now, what would both be? So if we were to run this again and immediately switch back over here, you'll see if we scroll all the way up, higher order collection proxy.
Ah, this is some of Laravel's sweet, sweet magic. So now, instead, we can say both resize to... Let's just copy all of these so we don't type them. So both resize, then we're gonna move them separately, and then we can say both visit, both wait for, both scroll into view, and we
should be good at this point. So now, what this is gonna do is this higher order collection proxy, when we call resize, it's gonna proxy under to every, uh, item in the collection. So it's gonna run resize on all of the items in the collection, which, in our case, are just the two browsers. So if we were to clear this out and run this again, we should see that both browsers are going to do the same thing.
Perfect. Hey, look at that. Um, the test didn't actually pass, which is not anything that we care about right now. So we can do this and say sleep for five seconds, and let's watch this again. I just deleted it. Let's watch this again. Dusk filter reverb. It's gonna open two browsers, and it's gonna do the identical thing on both browsers.
How cool is that? Zevia, not that great. We're poppin' off here, and I popped open a Zevia. Not that great. Higher order collection proxies, pretty awesome. I don't see a lot of people using them this way, um, because it's a little bit unorthodox. But remember, a higher order collection proxy proxies the next method to every underlying item in the collection.
And so sometimes you can use it for pretty interesting use cases like this. See ya.