تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Flattening Staff Arrays0:33

We need to figure out which of the users are due for a pay review. And that metric will be it's been at least a year since their last review. All right, so this normally would be pretty easy, but because the front of the restaurant staff members is separated from the back of the restaurant, it gets kind of tricky to do quick filtering. So what you might want to do is implement a custom. Remember, we're assuming we're not using a framework here or Laravel. This is just vanilla php. So to make it easy, we could create an array_flatten function where we accept the array.

This is just vanilla PHP. So to make it easy, we could create an arrayFlatten function where we accept the array. And then ultimately, I could say array_flatten($dataSet). And that should be my list of staffMembers, which we can ultimately var_dump for review. Sound good? All right, so it looks like we're going to filter over these. So I could say foreach $array as $key and $value. And then what we could do is say, well, if the $value for that iteration is an array,

And then what we could do is say, well, if the value for that iteration is an array, then what we're going to do is recall this function recursively. So I could say, well, if the value is an array, yep, I need to at some point call this function recursively, which is very cool. So we're going to give it the value. And then yes, we're going to have some kind of flattenedArray. So why don't we do this? Why don't we say flattened defaults to an array? And ultimately, that's what we're going to return here.

It's just a staff member. Okay, well, in that case, yeah, let's just push to it. And I think that should do it. So if we scroll down, we call our new function, we dump it to the console. And if we trigger this, there we go. We have our list of five staff members in a flattened array. Now, this is fine. Another option you might consider, if you want, is to pass by reference. So I could say, set the flattened array up here,

Finding Due Reviews3:29

So that's a fairly simple way to quickly implement an array flattened function. So now that we have this list, our next step is, we need to figure out which Users are due for a pay increase. And like I said earlier, if we take a look at this very quick and dirty object, we're going to check to see if the lastReviewDate is a year ago or more. So what we might do, if we scroll down, is we might say, well, for each staff member as member, and this is kind of the traditional way to do it. So we're going to do it like this. And then we're going to use the array functions. And then we're going to use collections.

And then we're going to use the array underscore functions. And then we're going to use collections. But anyways, we might say, dueForReview is an array. And by the way, this is often a sign that maybe there's a different way you can format these things, where you create an array, you perform an operation, and then you return that array. Sometimes there's a more dedicated function that you should use. Not always, but sometimes. So anyways, we could say, well, if the membersLastReviewDate, and by the way, that is a Carbon instance.

So anyways, we could say, well, if the members lastReviewDate, and by the way, that is a Carbon instance. So we can work with it as such. If it's older than maybe one year ago, then they very much are due for a review. So we could say something like, to start, var_dump $review the member's name. Now, we do have to create this variable. So let's say one year ago. And I'm just going to say Carbon::today()->subtract(1). All right. So I'm going to run this.

All right. So I'm going to run this. And we get review Susan and Mary. So those are the two people who are due for a review. Now, what we could do instead is say due for review and push the current member. And then finally, if we var_dump due for review and give this a run, we're now going to get a list of two staff members that need to be reviewed. OK. Now, further, what you might do at this point is say foreach. And you can see how this kind of gets tiring to do,

Encapsulating Review Logic5:23

Now, further, what you might do at this point is say forEach. And you can see how this kind of gets tiring to do, all these forEachs and kind of messy. But anyways, for each dueForReview as member, then, like we did before, we could do a var_dump. Or maybe we can expose this behavior on the instance, on staffMember. So maybe now a StaffMember exposes the behavior that they can be reviewed. And like before, we're just going to stub it out. Reviewing this name. You can make that a getter if you want.

Reviewing this name. You can make that a getter if you want. Whatever you want to do. So we give this a run. And once again, we get the exact same end result. Now, this is fine. But I can see quite a few things that I might do. For one, we're doing this member get their lastReviewDate and comparing it. So we are being responsible for figuring things out. Maybe we could always just say, well, if the person is due for review.

So we are being responsible for figuring things out. Maybe we could always just say, well, if the person is due for review. And then we could take this logic here and encapsulate it here. So return this user's last review date. And let me know if it's one year ago or more. But anyways, so now I could say, if the member is due for review. Or I'm sorry, is it just due for review? Anyways, if we run that, we got an import Carbon up there. So we'll grab that. Go to StaffMember.

So we'll grab that. Go to StaffMember. Run it again. And once again, we get the exact same thing. But we're just taking logic and moving it to where we think might be a bit more appropriate. And that means further, this can be removed entirely. But now we still have this thing where we are building up an array. So what exactly are we doing here? We're filtering the staff down to only the members who are due for review. So notice I said filter.

Filtering with array_filter7:04

We're filtering the staff down to only the members who are due for review. So notice I said filter. Well, maybe we could just use array_filter instead. Filter the staff down where the staffMember is due for review. Return member due for review. So now this is quite a bit cleaner than the foreach approach, in my opinion. And I can say due for review. Run it again. And we're still going to get the exact same thing. But now I do want you to note all of the stuff we did here.

Introducing Collections7:30

And we're still going to get the exact same thing. But now I do want you to note all of the stuff we did here. And just to perform a relatively basic operation, we had to implement a function ourself that flattened it the way we wanted to. And then we had to use the array_filter function. But then we can't just continue chaining each on top of that. So what we could do instead, of course, is pull in Laravel's Collection class. And this gives you a nice object to interact with your array, flatten it, do whatever you need to. Now, what we could do is pull in Illuminate\Support.

flatten it, do whatever you need to. Now, what we could do is pull in Illuminate\Support. And a lot of people do that. And that would be fine. But just be aware that it does include lots of things that aren't directly connected to your collections. So instead, there is a separate repository we could pull in that basically mimics exactly what Laravel provides. It's almost a copy and paste. So we could say composer require titan/collect.

It's almost a copy and paste. So we could say require Tighten collect. From the good folks at Tighten. OK. So now if I open up my sidebar, you can see that we have pulled that in. Now, specifically, I want you to take a look at the composer.json. And if we scroll down, we are autoloading this helpers file. So if we take a look at this, you now have access to any of these functions here. And specifically, one is called collect.

you now have access to any of these functions here. And specifically, one is called collect. OK. So let's give this a shot now. We're going to say, all right, here's your data, right? Well, let's just var_dump(collect($data)). And do note, before we run that, that's going to work because I'm already requiring my autoload.php file for Composer. If you're using a framework or something that's not Laravel, make sure you do that.

If you're using a framework or something that's not Laravel, make sure you do that. And you very likely already are. So if we give that a run, we can see that all of the items have now been wrapped within an Illuminate\Support\Collection object. So if you were to take a look at that, you now have access to all of these methods. And it took five seconds. So that means at this point, I really no longer need a array_flatten function.

So that means at this point, I really no longer need a array_flatten function. I don't have to maintain that. So what we could do instead is say, collect the data and then flatten it. And we'll say, save that to $staff, var_dump the staff members. And if we run this again, there we go. We now have a Collection instance. But you can see that it has flattened all of the people down.

We now have a Collection instance. But you can see that it has flattened all of the people down. So if you wanted to, you could say var_dump($staff) and pluck the name of each staff member. And we get a list of them. Great. So already, I don't have to maintain that function. And that's immediately a win in my mind. Less code you have to maintain and look at and write is usually better. Next, I want to filter the staff down to only those who are due for review.

Less code you have to maintain and look at and write is usually better. Next, I want to filter the staff down to only those who are due for review. So I can just call a filter method directly off of the collection object. And now because I do have an instance here, I no longer have to reference the array. It's implied, of course. So now we could say, due for review. And let's do a sanity check to see what that looks like. Run it again. And there we go.

Run it again. And there we go. So now we can see, yet again, these two people are due for review. So now even further, you don't have to call foreach on it. If you want, you could just say foreach member. And then we could say member review. Now I can get rid of that. And if we run it, we still get the exact same thing again. And of course, at this point, the variable is now superfluous. It doesn't need to be there.

Higher-Order Collections11:12

This does exactly what you need it to. Now, if you wanted to have fun and take this a step further, what you could do is use higher-order collections. So what I will tell you is this is perfectly fine, perfectly readable. Some people do get offended at the concept of these higher-order collections. They think it makes things less readable. And that's a valid concern, too. So as always, make up your own mind on this stuff. We're all adults. So higher-order collections give us kind of a nice,

But this is nice and clean, in my opinion. And it's much better than what we had before, when we were whipping everything together from scratch. All right, so that does it. So if you're working in Laravel, you have access to these collections out of the box. And there's, like I said, an enormous, so to speak, array of functionality that you can make use of. Now, if you're working outside of Laravel, once again, you can do composer require titanco/collect to pull in this behavior.

once again, you can do composer require titanco/collect to pull in this behavior. And you're all set to go. Have fun.

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