Introducing Nova Actions 0:00So, now we have a nice list of all our posts, we have a custom lens that we can use to get a specific view of our posts, and we have filters. Now, what if I want to perform a custom task whenever, let's say, I publish a post? So, for example, when I want to publish a post, I also want to send an email to a user. To do this, there is a concept in Nova which is called actions. And actions are just that, they are custom tasks that you can perform on one or multiple of your Eloquent models that are bound to your resources. So let's create a custom action now. The action that I want to create would be a publishPost action. So we can use the phpArtisan nova action command, and then give it the name of the Creating PublishPost Action 0:47The action that I want to create would be a publishPost action. So we can use the phpArtisan nova action command, and then give it the name of the action that we want to create. So in my case, it would be publishPost. Okay, action created successfully, let's take a look in Sublime. So now, inside of our app nova directory, we have a new subdirectory called actions, and in there we have this new publishPost class. So let's take a look at it. It has a handle method that receives some fields, and it receives a collection of models. So these are the models that you can perform your task on.It has a handle method that receives some fields, and it receives a collection of models. So these are the models that you can perform your task on. And then we can define custom fields, we'll cover that in a bit. So the most basic publishPost action would be something like, for each models as model, I want to update each model and set it to published. So here's published, will be true. Now we still need to associate this action with our nova resource. So similar to the filters and the lenses, our resource needs to know about this specific action. So let's go to our post resource, and in there, at the bottom, we have this actions method,action. So let's go to our post resource, and in there, at the bottom, we have this actions method, and it just returns an empty array right now. So let's create a new action. So we will say new publishPost and import the class. Now if I go back to Nova, and select at least one of our resources, we suddenly see this select action dropdown that contains our publishPost action. And if we select it and press the run button, we get a confirmation screen, and if we run this, the action runs successfully, and our post is suddenly published. Now let's change the message that we see whenever we publish a post. Customizing Action Responses 2:59this, the action runs successfully, and our post is suddenly published. Now let's change the message that we see whenever we publish a post. So instead of saying the action runs successfully, I wanted to say the post was published. To do this, let's get back to our handle method in our action, and in here we will return action, message, and then give it the string of the message that we want to see. So the post was published successfully, and if we try this again, publish another post, we now have our new confirmation message. You can also show danger messages by using something like action danger, and now if I run this again, this will give me a red message. So if you do something that is destructive, you can use the danger message instead.run this again, this will give me a red message. So if you do something that is destructive, you can use the danger message instead. If you want to not only show a message, but want the user to be able to download a file, you can use something like action download, and then in here point it to the file that you want to download. So something like storage path your file.csv for example. And last but not least, you can also redirect users away from your application or to a different part of your application by using the action redirect, and then give it the URL. So we can say https://nova.laravel.com, and if we now publish a post, we should get redirected to the Nova website.So we can say https://nova.laravel.com, and if we now publish a post, we should get redirected to the Nova website. Great! So this is how you can customize the actions and the result of your actions. Now, as I mentioned, let's say we want to perform some time-consuming tasks in our action, like writing an email. This could take some time, and the user does not really know about when the action is finished, and sending an email should really not happen in the main thread, so you should do this in a queued job. Luckily, queuing actions in Nova is really easy. Queuing Actions 5:30in a queued job. Luckily, queuing actions in Nova is really easy. All you need to do is go to your action class, and as you can see, it already uses the queuable trait, so what you need to do is just say it implements shouldQueue. As you can see, this class is already being used in the boilerplate, so you do not need to import it, and now whenever you run this action, it will be sent to the queue service that you have. So let's just add a sleep in here to simulate this, of 5 seconds, and now if I go back to Nova and run this action, we still see that the action ran successfully, so we do not get a redirect because now the action is being run on the queue, and I still need to startNova and run this action, we still see that the action ran successfully, so we do not get a redirect because now the action is being run on the queue, and I still need to start my queue worker, so hp-artisan-queue-listen, and now you see that this publishPost action was executed, and now after the sleep, it's done. So now if I refresh this post, it's now green. But now there is another cool thing with Laravel Nova and queued action, that you can show your user what is actually performing in the background, which action is being queued, and what's the status of this queued action. To do this, we need to add a trait to our post model. So what we're looking for is we want to see every action that happened on this specific Tracking Action Status 7:09To do this, we need to add a trait to our post model. So what we're looking for is we want to see every action that happened on this specific model. So if we go to our post model, there we have the searchable trait from Laravel Scout, and in addition, I want to add the actionable trait from Nova. So add actionable, import it, and now if I go back and take a look at one of my posts, at the bottom, I now see all the actions that were applied to this specific post. So now here you see that all these times I edited this specific post. And there you can do the same thing with actions. So now that we're on the detail page, I can also choose this action, and let's say publishPost,And there you can do the same thing with actions. So now that we're on the detail page, I can also choose this action, and let's say publishPost, run, and now on the bottom, you see that it's waiting for this publishPost action. And now it's finished. So now the queue is being processed, the action run, and Nova automatically updated this action for us. So this way, you can see the actions for your specific resources, you can see the status, and in addition, you also see whenever someone updates this specific resource. Similar to field authorization in Nova, you can also add authorization to actions. So you can define which users are able to see this action, and which users are able Action Authorization and Fields 8:49Similar to field authorization in Nova, you can also add authorization to actions. So you can define which users are able to see this action, and which users are able to run this specific action. So to do this, let's get back to our posts listing, and in our post resource, we have this action. And in here, I can just wrap this and say, canSee, then we have a function that receives the request. And then we can do something like return, request, user, and then perform a check on the user. So only if the ID is two, the user can see the post action, publishPost action.the user. So only if the ID is two, the user can see the post action, publishPost action. So now if I refresh, and fix this syntax error, and now select this, I'm no longer able to choose this specific action. So if I return true instead, select the post, I'm able again to publish this post and see this specific action. Now similar to the canSee method, you can also define a canRun method. And with this, you can control which user is being able to run this specific action on this specific resource. So if we add a canRun method, it receives a function which has the current request object,on this specific resource. So if we add a canRun method, it receives a function which has the current request object, and in addition, the current resource that is being used for this specific action. So post in our case. And now we can say something like return post ID is equal to three. So this would mean that only users can run this action on posts with the ID three. So if we go back to Nova, and select a post with a different ID, we still see that this action exists in here, and I can select it. But when I try to run this action, I get an error that I am not authorized to perform this action.But when I try to run this action, I get an error that I am not authorized to perform this action. And if I try this again with the ID three, which we allowed in our action, and we run this again, the action ran successfully. Now there's one more thing that I want to show you, and this is action fields. So if we go back to our publish post action, and let's remove the queuing for now, and also the sleep, then you see that we have this fields method that just returns an empty array. And with this, you can specify fields that should be visible inside of your custom action model.And with this, you can specify fields that should be visible inside of your custom action model. So for example, let's just provide a text field that we say is being called message. Import the text class, all right, and instead of redirecting, let's say message, and this is an empty string for now. So now, if I go back to Nova, refresh the page, and select the post with the ID three, select this action, press run, I now have the custom fields that I added to the actions field method available in here, and I can use them. So if I now say something like, this is my success message, and we want to use the value of this text field inside of our handle method, all we need to do is, since we get this fields,So if I now say something like, this is my success message, and we want to use the value of this text field inside of our handle method, all we need to do is, since we get this fields, we can just access them using fields, and then give it the name of the field that you want to access. So in our case, we call the field message. And then again, this is going to take the snake case approach, we use message. So now with this example, we would be able to write a custom success message for our action. So if we now run this, we see this is my success message, which is the message that I entered in this custom field.So if we now run this, we see this is my success message, which is the message that I entered in this custom field. You can use most of the available fields from all your resources inside of the actions. So you can use drop downs, or maybe the tricks, what you see is what you get editor, or even file uploads. And with this, you have a lot of flexibility for your actions. And you can ask your users for additional information that you can then use inside of your custom action. So whenever you need to perform a custom task on one or multiple of your models, Nova actions can help you with it.So whenever you need to perform a custom task on one or multiple of your models, Nova actions can help you with it.