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

Planning Status Counts0:00

Okay, let's work on getting the statusCount here for each of the statuses. So, we can do this in our statusFilters here. And we can have a new piece of state for every status that we need the count for. So, in this case, we need 5. So, for example, we can make a new one here for statusAllCount. And then, in the mount method, we can do this statusAllCount is ideaCount. And then we can do the same for the other statuses as well. So, for example, for all.

is Idea count. And then we can do the same for the other statuses as well. So, for example, for all it would be Idea where status_id is 1. And then we can count that. And that would work. So, we would have 5 extra queries here. And that's fine. These queries run really fast. But if you want, do it in one query. So, we can minimize the amount of queries that we're running. We can do that as well. And that's what I'm going to do here. So, let's get rid of this. And let's make a or let's change the one we currently have to statusCount.

Single-Query Count SQL1:04

rid of this. And let's make a or let's change the one we currently have to statusCount. And actually, let me just change this. And let's work in raw SQL for now. So, let's open up SQLAce. And this is how you would get the current count. And I have an alias for all statuses here. And right now, there are 101. So, we can just add the different cases for each status. So, it would be something like count case when statusId equals 1 then 1.

case when status_id equals 1 then 1 end. And then we can alias it. So, this one is open. And that would work. And I believe there's no comma for the last one. There we go. And we can do the same for the other statuses. So, let me just paste those in. So, there we go. We counted all of the statuses in one query. So, let's go ahead and do this in Laravel. So, for the status count, we can do Idea and we can do selectRaw. And

idea. And we can do selectRaw. And for the first one, it's going to be count(*) as all_statuses. And we can just chain on the other selectRaws as well. So, I'm going to add a query here just so the selectRaws line up. Okay. And then we can add the other ones as well. So, selectRaw count(case when status_id = 1.

count case when statusId equals 1. Then 1 end as open. And let me just paste the rest in. Okay. So, I pasted the rest in. And let's change it to an array. And let's see if this is correct. So, let's just dd everything to see if we did this correctly. statusCount. Okay. And let's see what we get. So, back here. Idea not found.

Move Logic to Model3:12

see what we get. So, back here. Idea not found. toArray. I think we have to call first. First. Okay. So, we do get an Idea here. Or an array, I mean. And the counts look correct. So, I'm going to move this to the Status model. You can leave it here if you want. But it makes sense to put it on the Status model to me. So, we can do something like. Actually, let me just grab this. Because that's going to go in there. We can do something like Status

So, we can do something like. Actually, let me just grab this. Because that's going to go in there. We can do something like status::getCount. So, it's going to be a static method. So, let's go ahead and put it on the Status model. Say use Status as well. Okay. So, Status. Let's say getCount. And it's a static function, like I said. And we can just paste that in. So, return $this. And yeah, that should be good. So, we can go ahead and

Wire Counts into Filters4:16

So, return this. And yeah, that should be good. So, we can go ahead and use it on the view here. So, let's go to statusFilters. And we can make use of that variable here. So, I'll just add it for this one. And I'll paste the rest in. So, it should be statusCount. And we have it as an array. And this one is allStatuses. Okay. So, let me just grab this. Let's see if this works first. So, allStatuses is 101. And that's correct.

Okay. So, let me just grab this. Let's see if this works first. So, all statuses is 101. And that's correct. So, I'm just going to paste in the rest for the other statuses here. Okay. So, I added the rest in. Let's see if this works. And it looks like it does. And as you see, our queries only went up by one. If we did it the other way, we'd have five extra queries, which is fine. Like I said, these queries run really fast. But you can see the one that we just made right here. So, yeah, that looks good. We'll test everything in the next video. I just want to do a quick fix from the last video. So, pagination. Like I said, we'll fix it and make it work with Livewire.

Fix Pagination Query String5:20

in the next video. I just want to do a quick fix from the last video. So, pagination. Like I said, we'll fix it and make it work with Livewire or the Livewire pagination in a later video. But for now, so it works on the default all status. But if I go to a different status, right now it's on considering. If I press next, it will go back to the all status. So, it doesn't maintain the current query string. So, to do that, just go to our ideas index, and we'll make a quick fix here. So, I'm just going to comment this out for now. And I'll paste in the correct code that maintains the query string. So, again, when we use Livewire,

here. So, I'm just going to comment this out for now. And I'll paste in the correct code that maintains the query string. So, again, when we use Livewire, we'll put this back. But for now, we have to add this appends method and pass in the query string. And that should maintain the query string. So, again, back here, this should still work. But now the other statuses should work as well with pagination. Okay, cool. So, let's go ahead and make a commit here. This is episode 25. Okay, commit episode 25.

Commit Changes6:24

25. Okay, commit episode 25. Say status, filters, count.

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