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

Spotting Duplicate Queries0:00

Now take a look at the commit from our previous episode. So we talked about this shortly, but we have this section in our ChannelsController where we keep adding what amounts to a filter, or a scope. So when we fetch channels, we want to make sure that we only grab the active ones. So let's make sure the archived attribute is set to false. And then in ThreadsController, when we show the form to create a new thread, we're just basically running the same query again. Next, back in code, if we go to our AdminChannelsController, you'll see when we show all of them once again. So we have basically three different occurrences of the same query. Why don't we tweak that in this episode? So I'm going to go to ChannelTest, and I think what we're going to do is add a global scope. So we'll say here, archived channels are excluded by default. So let's see, we'll have one channel,

Writing Global Scope Test0:46

and I think what we're going to do is add a global scope. So we'll say here, archived channels are excluded by default. So let's see, we'll have one channel, and then another channel. However, the archive status should be set to true. Okay, and we don't need that variable. So now if we were to fetch all channels, well, what we want to say is by default, only one should return because it'll detect, ah, this is archived, you probably don't want it in the results. So if I were to say Channel::all(), actually, let's do count(), this should return one. So let's give that a run. And of course, it fails. But actually, you know, not for the reason we thought, we got to make sure that we import Channel. All right, one more time. And now it fails because we are returning two. Okay, let's add a global scope. Now we can do this either as a dedicated class,

Implementing Global Scope1:36

All right, one more time. And now it fails because we are returning to. Okay, let's add a global scope. Now we can do this either as a dedicated class, or we can do it on the boot method. So let's add a protected static function called boot. We'll defer to the parent. And then if you're curious, Laravel docs global scope. Sometimes you need a quick refresher. All right, so right here. Yeah, so we could create a dedicated class, which could be useful. But we can also do it as an anonymous function here. So something just like this is what we need at a global scope. And we'll say active. And now think of this as a scope that is applied no matter what. So in this case, we want to say where the archive status is set to false. Okay, let's give our test a run again. And looks like just a type in issue, we don't even need that. Run it again, and we do get green. So that is working,

false. Okay, let's give our test a run again. And looks like just a type in issue, we don't even need that. Run it again, and we do get green. So that is working, which means think about it, if we come back to console.test, and we go to our settings and the channels, let's archive one of these. So now back in SQL Pro, we can see a period is archived. And if we go to channels, well, just to be safe, let's clear out our cache. All right, one more time refresh. Anyways, we're not going to see that in the result set, which means if we go to, for example, ApiChannelsController, we no longer have to be explicit here because it's going to be applied by default. All right. So once again, run that again, refresh, and I'm not going to see that a period one. Okay, perfect. So that means in a couple of different places, ThreadsController create, we can simplify

Bypassing Scope in Admin3:14

All right. So once again, run that again, refresh, and I'm not going to see that period one. Okay, perfect. So that means in a couple of different places, ThreadsController create, we can simplify things a little bit. Next though, you'll notice here in the dashboard, I do want to see even archive channels, but now they're being applied. So as it turns out, we can basically say ignore any global scope I've added here just for this particular query. And I think that's what we want. So we'll go to AdminChannelsController, and then right here without global scopes. So we'll give that a refresh again. And now we are seeing the archive channels only in this place, but anywhere else, we're not going to see those archive channels. But now one other thing, you'll notice that up here, our channels are sorted in ascending order by their name, and the same will be true here. What if, let's go to ThreadsController, be nice if we didn't have to deal with that.

Adding Default Ordering4:04

that up here, our channels are sorted in ascending order by their name, and the same will be true here. What if, let's go to ThreadsController, be nice if we didn't have to deal with that. So why don't we just say channel all. And once again, let's just assume in almost every scenario we want the channels returned in that order. Okay, right now, if I give it a refresh, now they're no longer in ascending order. But if we go back to our channel global scope, we could do it here, orderBy the name in ascending order, give that a refresh. Give that a refresh, and now that's back. And then we also have this use case up here. So that was an ApiChannelsController. Yeah, and like I said, if this query were more in depth, I think we should extract it to a dedicated class or a repository. However, because we've made a few tweaks like this, we basically returned it to channel all, which is so simple, I'm not sure it

Reviewing Changes and Committing4:51

I think we should extract it to a dedicated class or a repository. However, because we've made a few tweaks like this, we basically returned it to channel all, which is so simple, I'm not sure it even warrants being extracted. Anyways, once again, we'll clear the cache. And if we switch back, give it a refresh. Now that will be in ascending order as well. And if we go to channels, that's still fine because we've disabled all global scopes. So let's take a look at GitHub. We've added a boot method to apply our global scope. On our AdminChannelsController, we just had to be clear, don't apply any of those global scopes in this particular case. However, for our API, we've vastly simplified the query. On ThreadsController, we've simplified the query. And in our test, we just have a little bit of assurance that it does what we expect. So if we come back, let's do a full run of phpunit. That's good. So now let's commit and say

the query. And in our test, we just have a little bit of assurance that it does what we expect. So if we come back, let's do a full run of phpunit. That's good. So now let's commit and say use global scope for active channel results. And we'll sync that up. And we'll now find it right here. Oh, and it looks like styleci picked up a mistake and just a line break there. Anyways, here is what we ended up with. Looks good.

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