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

Creating Verify Action0:00

So, let's begin our foray into actions by making it a single click to verify an unverified Review. So, inside our review resource, right down at the bottom, we have our actions method where we can define the actions for reviews. Now, usually you define actions inside a class, but if your action is simple and single use, you can actually define it using a closure. In our case, we can import the LaravelNovaActions action class, and I'll call the using method. We need to define the title of the action, I'll call it verify. Then we'll pass a closure which will receive two parameters. The first is an actionFields object. This contains any information that has been passed to the action. We'll cover that in depth in another episode. The second item that is passed is a collection of models.

We'll cover that in depth in another episode. The second item that is passed is a collection of models. So, the User will have selected one or more models, in this case reviews, that they want you to take action on. So, what do we put in the body of the closure? Well, whatever you want, really. It's just code. You can take any action you desire. In this case, we're wanting to verify our collection of models. Now, we could loop over each model, but that's going to be a lot of database queries. I think, instead, I'm going to grab the reviews.

Bulk Update Verified Reviews1:25

Now, we could loop over each model, but that's going to be a lot of database queries. I think, instead, I'm going to grab the reviews. I'll ask for keys in the models plucked down to just their IDs. And then I'll call the update method, and I'll set the verify.column to now. Of course, if the review has already been verified at an earlier date, I don't want to change that date. So, I'll limit it to only reviews where the verify.column is currently set to null. And that really is all our action needs to perform. Now, on any review, from the three-dot menu, we see a new verify action at the bottom, which we can click and run in order to mark this review as verified. The action can also be taken from the review index.

Introducing Standalone Actions2:09

which we can click and run in order to mark this review as verified. The action can also be taken from the review index. I could select a few reviews, and then in this dropdown here, I can click verify to run against all of the models that I have selected. Of course, not all actions will require you to select models. So, for an example, in our case, let's say you have cleaned up the reviews you're happy with, but you have a ton of reviews that are just spam. You want a single button that will allow you to delete all of those reviews. You don't want to have to select things individually. That is called a standalone action, and it's just as easy to implement.

Generating Destructive Action Class2:44

You don't want to have to select things individually. That is called a standalone action, and it's just as easy to implement. However, because it deals with destroying things, I think we'll want to create it as a separated class. Let me show you why. From the terminal, I'll type php artisan nova:action. We'll call it destroyUnverifiedReviews, and I'm going to tag on a flag called destructive. Now, when an action is marked as being destructive, it will pop up a warning modal in Nova, which we'll see in just a second.

Now, when an action is marked as being destructive, it will pop up a warning modal in Nova, which we'll see in just a second. For now, let's go ahead and create destroyUnverifiedReviews, and let's open it inside our IDE. Note that we're extending destructive action because of the flag we passed, but the handle method looks very similar. Action fields and a collection of models, the same as we implemented in the closure. The big difference with a standalone action is that you can just ignore these models, because if it's standalone, it's guaranteed to be empty. So, in our case, we want to take any review

because if it's standalone, it's guaranteed to be empty. So, in our case, we want to take any review where the verified_at column is currently null, and we can just delete that in a single SQL query. So, a very simple action, but very powerful. We'll now register this action in our Review resource. I'm going to create a new instance of destroyUnverifiedReviews, and I'm going to tag on the standalone method in order to make it standalone. Let's see how this looks in the browser. When I refresh the page, you'll see we have a new three-dot menu next to createReview.

Adding Confirmation Text4:16

Let's see how this looks in the browser. When I refresh the page, you'll see we have a new three-dot menu next to createReview with our destructive destroyUnverifiedReviews action. Note the red text color. When I click it, I get a warning modal asking me if I'm absolutely sure I want to run this action, and I have to click this big red button if I want to do so. Now, you're free to customize the text you see here. If we go back into our resource, I can tag on a confirmText method, and I can put whatever I want in here. Are you sure you want to run this action?

and I can put whatever I want in here. Are you sure you want to run this action? This action cannot be undone. And after adding that, if we run this action again, yeah, this action cannot be undone. So, easy to customize the action so as to get across that it will do bad things if you use it without caution. Of course, I'm happy to run this action. I'll click Run, and now all of the reviews we have left are indeed verified, as you can see, and we only now have 13 reviews.

Recap and Next Steps5:08

I'll click Run, and now all of the reviews we have left are indeed verified, as you can see, and we only now have 13 reviews. So, we can create standalone actions against any resource where we're not actually interested in selecting particular models. We just want to run some arbitrary code, which is likely linked to the resource we're currently looking at. That's the basics of actions. We can create a class, or we can use an inline closure. We can make them destructive, we can make them standalone, and we can customize the text that's displayed and shown.

We can make them destructive, we can make them standalone, and we can customize the text that's displayed and shown. But we're only really scratching the surface of what actions are capable of. Let's dive a little deeper.

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