Handling Comments Table0:00
(upbeat music) Okay, now we've got a bunch of comments coming in from our front end. And so I'm gonna show you how we can handle that in our comments table and show you some of the new things that are available in Filament 4 or 5 that weren't available in Filament 3. So we're here in our comments table and we've got the ability to see whether comments are approved.
and we've got the ability to see whether comments are approved. Right now, all of them are not approved. A couple of things just to note, if I were to search here for something that is gonna make the table empty, you can see I actually still see what the columns are in the table. And this is something that's new. I think it's nice, whereas this used to go away
Adding Empty-State Actions0:41
And this is something that's new. I think it's nice, whereas this used to go away and you would have no idea what might show up here if it were empty. So this is nice. The other thing, and this has been available in Filament for a long time, but this is something, if people are new to your app and they don't have anything in a certain table, we can go into the comment and on the table,
and they don't have anything in a certain table, we can go into the comment and on the table, we can give a empty state actions. And you can just pass your create action in here. And it's really nice. So all I have to do is that. And when I refresh the page, I'll just do one more Z here. Now, it tells me I can create a comment and I can click here to actually create that comment.
Now, it tells me I can create a comment and I can click here to actually create that comment. So really nice way to give your users just a little more indication. It does the exact same thing as this button up here, but just letting them know and making it very easy to see, oh, there's nothing in this table. Let me add something. So that's one change that's really nice. If I remove that, everything goes back to normal.
Reordering Table Columns1:37
So that's one change that's really nice. If I remove that, everything goes back to normal. Another thing that's brand new in columns is you have the ability to reorder columns. So I could put approved right here at the very front and apply that. And now it reorders for me. So your users really have a lot of ability to customize their table however they like. You can reset it to the way it was supposed to be.
Bulk Actions Authorization1:54
to customize their table however they like. You can reset it to the way it was supposed to be. Oh, I'll reset and there we go. So everything works just the way it should. So that's the ability to reorder columns. And the last thing I really wanna cover and we'll spend most of our time here is the updates they made to bulk actions. So bulk actions previously in Filament were something that was a little risky to do
So bulk actions previously in Filament were something that was a little risky to do because if you were trying to update a hundred records at a time, we've been harping on over and over, does the policy allow you to do that thing? And in Filament 3, there was no built-in method to check. Is this user actually allowed to make all these updates they're trying to make through a bulk action? Now, Filament 4 and 5 have the ability to check every single record in that bulk action and check,
Now, Filament 4 and 5 have the ability to check every single record in that bulk action and check, am I allowed to actually make this change? And if I'm not allowed, should I keep going and make the changes to the ones I can? And the answer is yes. So let me show you how you can do bulk actions but also make sure that bulk actions only happen if they're allowed to happen. So we're gonna go back to our code
if they're allowed to happen. So we're gonna go back to our code and we have bulk actions here. I'm going to create a custom action here. So I'll say bulk action. Make, we're gonna call it a prove, let's see what we got here. Let me make this a real long closure function. It's not actually an array, it's a collection of records. So we'll do an illuminate support collection and let's make this a little bit nicer.
So we'll do an illuminate support collection and let's make this a little bit nicer. We'll say records each and we're gonna try to update as a prove to true. And so this is kind of a start of where we want to be where if we do this bulk action, we're gonna require confirmation, the color is gonna be success. So let's look at what we have right now. Now that we have one bulk action,
So let's look at what we have right now. Now that we have one bulk action, you have the ability to select records and the bulk actions come up. This says approve if we wanted to, the color is success, we could remove that. I think it looks weird that it's green. So that looks a little bit better. Or if we wanted to add an icon, we can do that. It's all the same throughout filament.
Or if we wanted to add an icon, we can do that. It's all the same throughout filament. And if I were to do this, it would approve all eight of them. And there's no authorization anywhere here. So there's two ways that I would recommend you authorize this. Number one, you can do authorize. And basically this could be, are you even allowed to see the bulk action at all? So if we just return false here, and we probably want to make a closure,
So if we just return false here, and we probably want to make a closure, if I do this and refresh, now I'm not allowed to even try to do a bulk action. So that's one way to make sure if no one is doing bulk actions that they should not do, but that's always been there. The new thing is going to be authorized individual records. And so what you need to pass in here is the name of the method in the policy that needs to be called.
And so what you need to pass in here is the name of the method in the policy that needs to be called. So let's create a new approve method on our policy. So we'll go to the comment policy here. And at the very bottom, we're going to add a new one for public function approve. Let's do a random number here. So a random number, either one or two. If it's one, we're going to approve it. If it's two, we're not.
If it's one, we're going to approve it. If it's two, we're not. So we should get about half of these to fail. And now if I go back to my browser, and I can see if I try to approve all eight of them, actually, I'm not ready to do this yet. I gotta go back to our comments table. So I'm not ready to do this in the browser yet because even though this will work, I'm gonna have no idea what happened
Adding Result Notifications5:52
because even though this will work, I'm gonna have no idea what happened until I kind of look in the table and see what has been updated. Let me go show you in the filament docs really quickly. Let's go back over here. And in the actions, we have bulk actions. I would find this by just searching bulk action up here. And you have authorizing bulk actions. So this is what we want to see.
And you have authorizing bulk actions. So this is what we want to see. And we're doing this right now, but we really want to make sure we implement the notifications to tell a user what the heck happened. And so we can give it a success notification title and a failure notification title. I'm just gonna copy this and we'll kind of change it according to what we need. Let's move this here.
according to what we need. Let's move this here. So all the authorization stuff is together. We're just going to say all comments approved if everything passed. If any one, even if just one of these eight does not succeed, then we're gonna get this notification that says, only this many comments. Approved, get rid of that. And then if every single one of them fails,
Approved, get rid of that. And then if every single one of them fails, we would say no comments approved. And so this will allow us to actually be notified of what the heck happened. So now if I go back to the browser, I think we're ready to run this. I'm gonna refresh. If I click approve, oh, now I'm getting this over here as a slide over.
Confirm Modal and Deselect7:16
If I click approve, oh, now I'm getting this over here as a slide over. So generally, you remember back in the beginning, we like slideovers or modals to be slideovers by default. In this case, it doesn't make sense to have a slide over for a confirmation modal. So we do have this requires confirmation, which is why that's showing up in the first place. But we just will add slide over here as false. And now we can actually do that with a modal.
But we just will add slide over here as false. And now we can actually do that with a modal. So I'm going to try one more time. Bulk action approved. I'm gonna confirm it. And four out of eight comments were approved. You can see these four were approved. The other four were not. And now I'm actually notified of what happened. And I can be sure that the ones
And now I'm actually notified of what happened. And I can be sure that the ones that I was able to approve actually were approved. Now, one other thing that I think is kind of weird is that I just ran this bulk action and every record is still selected, especially because in most cases, the bulk action is going to succeed. You probably don't need them selected anymore. So there is one more method we can chain on here.
You probably don't need them selected anymore. So there is one more method we can chain on here. We'll just say deselect records after completion. And so I'll go back and I'll highlight all of them and try to approve all of them this time. And when I click approve, now they are deselected. So in this case, three of eight comments were approved, which isn't probably exactly the correct message because there's probably just three of the eight comments past authorization, but some of them were already approved.
because there's probably just three of the eight comments past authorization, but some of them were already approved. So again, you can do some more things to make sure that you're only approving the ones that are not approved. We're not going to go into that in the video, but you could do something like that. So that's bulk actions and authorizations. It's really powerful here in filament. And that's most of the changes that have been made to tables.
It's really powerful here in filament. And that's most of the changes that have been made to tables. If you're, again, interested in how tables work in filament generally, go watch my filament three course, you'll get a lot more information there. But there is one more change about tables and it has nothing to do with eloquent models. Filament now in filament five and also four gives you the ability to create a table
Filament now in filament five and also four gives you the ability to create a table without using an eloquent model as the backing. And that can be really powerful. So in the next video, I'm going to show you how to use tables in that way.
