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

Action scenario and injection0:00

Well, if you're anything like me, your mind is now racing with ideas for actions. But the more scenarios you think of, the more you realize you usually need more than just code. You need user input. Let me give you an example in our case. I've created an action called sendCustomerDiscount. What it says on the tin, we select customers and we choose a discount we want to send them. It will email them a discount code they can then redeem and use in store. I've injected this DiscountService that we have in our application. If you're wondering how injection works, by the way, well, inside the resource under the actions method, I simply use the resolve helper that Laravel ships with to pull this out of the service container.

If you're wondering how injection works, by the way, well, inside the resource under the actions method, I simply use the resolve helper that Laravel ships with to pull this out of the service container. Now, in the handle method, I want to make use of this discount service. So, for each model, I'm going to call this discount service. And it has an email method that expects a customer and then the discount we want to send them. Now, of course, I could hard code, say, 15% discount. But that's not really going to work for us, is it? We don't want to hard code the discount. We want the administrator to be able to choose that discount at runtime. So, how do we do that?

Defining action input fields1:18

We want the administrator to be able to choose that discount at runtime. So, how do we do that? Well, there are two things in the action class that we've yet to touch on that come together to allow us to define user input. The first is this actionFields parameter and the second is the fields method. We'll start with the fields method. The fields method works very much the same way as the fields method on a resource. We can define any number of fields that we want to show in the front end. So, I'm going to use the Number field that ships with Nova. I'll call it discount. And then we can add methods to this that make sense.

Validating the discount field1:53

I'll call it Discount. And then we can add methods to this that make sense. Maybe the minimum discount is 1. The maximum discount might be 100. It's important to validate, so we'll add the rules method. I'll say it's required. It's an integer. It has a min of 1 and it has a max of 100. And that's our field defined. Once you've defined your fields, you can access them using the action fields class passed to the handle method.

Using fields in handle2:14

And that's our field defined. Once you've defined your fields, you can access them using the actionFields class passed to the handle method. So, I'm going to replace this hard-coded 15 with fields and then I can use object syntax to grab the discount property. If you prefer, you can also use array syntax instead. With our action updated, why don't we jump into the browser and see what this looks like. Now, I've already configured our discountService to output information to array. So, I'll pin that to the screen here. I'm going to select our first customer and from the actions dropdown, I'll send the customer a discount. We get the pop-up modal as normal, but we also have our defined field. And I can enter any information I want in here.

Running the action in UI2:52

We get the pop-up modal as normal, but we also have our defined field. And I can enter any information I want in here. Let's give him a 15% discount, for example, and we'll run this action. And you can see we are sending a discount email to the correct email and here is our user defined information, 15%. We could send him another discount email. Let's up it to 25% this time. And now you can see I'm sending a 25% discount down here. Now, you're not limited to a single field here. You can add as many fields as make sense for your action. But I do think it's important just to keep in mind that actions should be quick and easy to run.

Defaults and action UX3:26

You can add as many fields as make sense for your action. But I do think it's important just to keep in mind that actions should be quick and easy to run. So, don't flood the action with fields if you don't need to. And if you do, make sure to provide sensible defaults. You can use the default method for this on a field. And obviously I could say by default I'm going to add 10 in there. So, if the most common discount we give is 10%, I'll use 10 in the default method. And now when I select a User and go to send a customer discount, it pre-fills the input with 10 for me. And I can just very quickly run the action without having to change anything. Now, you may not have realized it yet, but there is a real problem with the action we've just created.

And I can just very quickly run the action without having to change anything. Now, you may not have realized it yet, but there is a real problem with the action we've just created. We'll talk about it and fix it in our next episode.

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