در حال بارگذاری ...

Higher Order Messages Intro0:00

Next up is higher order messages. So I have this Post class here, and imagine that we have a method to archive it. And all that's going to do is trigger this update, and then we will set the active column to false. Now, let's use php artisan tinker for this. Let's say that we have a bunch of posts, so we will grab them. And what we want to say is, for this subset of posts, for every single one, we want to archive them. Okay, well, traditionally, you could use a foreach, or you could call the each method on your collection instance, and then you could say for every single post, we're going to call post::archive. So if we run that, and sure enough, we've updated every post to have an active status of false. So that'll work, for sure. However, we can now use shortcuts, which we call higher order messages. That sounds fancy. They're just shortcuts. In this particular case, we're saying for each post, call an archive method. Okay, well, now we can just say each. We're going

Using each->method Shortcut0:48

higher order messages. That sounds fancy. They're just shortcuts. In this particular case, we're saying for each Post, call an archive method. Okay, well, now we can just say each. We're going to use this dynamic property here, and then we're going to call archive. And we get the exact same thing as we had before. So let's do this. Let's set up the inverse now. We'll say activate, and then that's going to do basically the same thing, but instead of false, it's going to set active to true. Okay, let's run it one more time. We fetch our posts, and now I can say posts for each one, I want to call an activate method on the Post that is passed. So it's just a bit of shorthand, and honestly, this sort of magic will drive certain people insane, which is probably why you should take a look. Now you'll find that this will work on a number of collection methods. Consider the example we still have for posts, right, where we just want to pluck the title. Well, as you know,

Mapping Properties Shortcut1:32

take a look. Now you'll find that this will work on a number of collection methods. Consider the example we still have for posts, right, where we just want to pluck the title. Well, as you know, we have a pluck method where we could say pluck('title'), which is great, and this is what you would use. However, maybe you don't know about that, and you want to say titles, and you're going to do it the long form way. Well, you could say posts, map over them, and for each one as post, I'm going to return the title of the post. This is the long form way, and we get the exact same thing. However, what we could also do is say posts, map over them, and for each one, we're going to return the title. And once again, we'll save that to a titles variable. Now we get the exact same thing again. So in this example, yeah, of course, there's already a pluck method for you, but you can see the flexibility here and how convenient it can be for basic things like this. Now, this is going to

Applicable Methods and Guidance2:16

So in this example, yeah, of course, there's already a pluck method for you, but you can see the flexibility here and how convenient it can be for basic things like this. Now, this is going to work on a ton of methods like contains, or each, or sum, or filter, or map. There's a lot here. You can go through the documentation. Don't force yourself to implement this if it feels like it makes it less understandable, but for basic little one-offs, I think it's really useful.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟