Validating Update Requests0:00
Next up, we have higher-order tap, which is kind of a fancy term to say when you call the tap function in Laravel 5.5, you can do some extra stuff now. Okay, let's review an example. I have the typical PostController here, and we have a method to edit a given post. So if we view this in the browser, you have pretty standard stuff. So we fill it out, when we click on the button, that will, if I switch back, hit this update method. Okay. So in 5.5, typically what you're going to do is first validate the request, and we can now do that doing request()->validate(), which I feel like is a little more object-oriented.
So in 5.5, typically what you're going to do is first validate the request, and we can now do that doing request->validate, which I feel like is a little more object-oriented. It feels a little more natural. So we could say, yeah, in this case, the title, of course, is required, and then the body is required as well. So that's great. But now, if you missed it earlier in this series, we can also return those validated fields or attributes as a variable. So now this can be really useful, because if the user tries to sneak in, here's my edit form, if the user tries to sneak in a hidden input, because they're trying to manipulate
So now this can be really useful, because if the User tries to sneak in, here's my edit form, if the User tries to sneak in a hidden input, because they're trying to manipulate you or sneak something into your table, yeah, when we do it this way, the only fields that will ever be saved to attributes will be title and body in this case. So if they sneak something in, it doesn't matter, because it's never going to be returned as attributes. So now we could just say, post, I want to update it with the given attributes, and then either return a redirect, or sometimes if this is an AJAX request, sometimes you'll return the model itself or a dedicated response, whatever you want. So we're going to talk about this right here, because this is a really good use case for
Traditional tap Usage1:58
Now I'm going to show you the traditional version using tap, which you can use in 5.4, and then we're going to see how it's a little cumbersome, and then we'll take a look at how we could do it in 5.5. So yeah, if you're not familiar with tap, here's the basic implementation. You're going to give it a value here, and then you'll provide a callback function or closure that will accept that very value, like so. And that's the basic blueprint. So you call tap, you give it a value, you give it a closure here, and then behind the scenes, this tap function is going to trigger that very function and then pass through this value here.
scenes, this tap function is going to trigger that very function and then pass through this value here. All right? So that means I could say tap, give me a new Post, and I want to store that as post, and then within here, I'm going to perform some logic. Okay? So to give you an example of how that looks, this is the 5.5 implementation, but if I get rid of this, this is how it would look in Laravel 5.4. So yeah, here we give it a value, we give it a callback function. This is how it is in 5.4.
So yeah, here we give it a value, we give it a callback function. This is how it is in 5.4. I'm sorry. Anyways, so notice behind the scenes, it's going to trigger your callback function and pass through the value you give it, and then ultimately it returns the original value. This is a key thing to understand here. So now, if we run this code right here, what's going to be returned is the post value here. Okay, so that means, yeah, if we wanted to, we could do something like this, where we say tap post, and then down here, I will update it, but we got to do this to make attributes available.
say tap post, and then down here, I will update it, but we got to do this to make attributes available. So you can see it's a little clunky to do it in this way, which is why nobody ever does. But nonetheless, because we're using tap and we're giving it the post variable here, that is what's going to be returned from the function, which means I could return this rather than having to do it in two separate steps. So let's take a look at that now. Refresh, changed again, update, and you'll see, yeah, we get the exact same thing as we had before, but now we're using tap. I would say it's not as clean, but in some cases, even if you don't have to do it, it
Higher-Order tap in 5.53:51
we had before, but now we're using tap. I would say it's not as clean, but in some cases, even if you don't have to do it, it can be nice to kind of group-related logic or nest-related logic like you see here. But yeah, I would agree with you, in this case, it kind of feels like more trouble than it's worth. However, in Laravel 5.5, I'm going to bring back the new implementation, you'll see that the callback function is now optional. So if null callback, then it's going to return a higher-order tap proxy. What the heck does that mean? Okay, well, let's see it working, and then we're going to review that.
What the heck does that mean? Okay, well, let's see it working, and then we're going to review that. So if we come back, I could now rewrite this like so. I could say tap, post, but now I'm going to omit the callback function, and instead just do something like this. This should kind of blow your mind here. So now I can return this, and everything's going to work. All right, let's give it a shot, put it to the test. One more time, changed again, again, we run it, and yeah, we get the exact same thing before, but now we're leveraging the higher-order tap functionality in Laravel 5.5.
Understanding tap Return Value4:48
One more time, changed again, again, we run it, and yeah, we get the exact same thing before, but now we're leveraging the higher-order tap functionality in Laravel 5.5. So if you're anything like me, when you first see this, you're like, wait a minute, how does that even work? What's going on here? Okay, well, here's the thing to understand. Whenever you call tap, if you're a little blurry with tap, which I think in the Peach Speech Humanity, a lot of people are, the way I like to think of it is tapping something on the shoulder. So we're going to tap post on the shoulder, and ultimately, that's what I want to be returned.
on the shoulder. So we're going to tap Post on the shoulder, and ultimately, that's what I want to be returned. That's what I want to turn around and be returned. So make sure you kind of lock that in your head. When you call tap, the very first thing you pass to it is always what's going to be returned. All right, so that means when I said return tap, well, post is the variable that's being returned from this. But now it gets tricky, because we called update after that, and you may know that update or save on an Eloquent model, those don't return the model. Those return a Boolean.
How the Proxy Works5:43
or save on an Eloquent model, those don't return the model. Those return a Boolean. So if we see this, we can see tap post update, but we know update returns a Boolean, so we think this entire call is going to return a Boolean, but no, we got the full model. All right, so let's figure out what kind of magic's happening behind the scenes to make this work. So don't squawk just yet. Give it five minutes before you decide whether or not you like this. Okay, so we didn't give it a callback, which means we return higher order tap proxy. All right, so now we have our target.
Okay, so we didn't give it a callback, which means we return higher order tap proxy. All right, so now we have our target. This is now going to accept the post variable that we have right here. And then what else is going on? So you'll see it's just registering a magic method. So now we can see when I call this right here, let's review this all together. When I call tap post, what's getting returned now is an instance of this higher order tap proxy. Let's take a look. Run it.
Let's take a look. Run it. Yeah, we now have an instance of higherOrderTapProxy, and you'll see the target is equal to that post variable that we have here. Okay, so now what happens when we do things like this, where we call an update method on it? All right, well, we can see right here this call method will fire when we pass any method name that is not defined on the class, just like update. All right, so we call this, and you'll see that it just delegates. It just delegates to the original target.
All right, so we call this, and you'll see that it just delegates. It just delegates to the original target. So in this case, it's delegating to postUpdate. And then finally, you'll see it returns the target just like any other tap implementation would be. So yeah, in this case, we're basically saying postUpdate with the parameters. And then finally, return post. Once we break it down, this is ultimately what we're doing here. All right, so think about, this is actually really cool. And I think for many people, your first reaction is, too much magic.
