Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
FREELessons:16Length:2.1 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.4 Saving Tickets

In the previous lesson, we created our submit ticket form, and we did so as a view component. And we even tested it, it works. Well, at least as far as the actual form is concerned. There was nothing in the server side part of the application to handle that request, so of course that is the next logical step. We want to accept that request and actually store a ticket. So we need a controller for that, so let's go ahead and use artisan to make a controller, and let's just call it TicketsController. Just because that makes sense. We're going to be working with tickets. Let's also make this a resourceController because, well, this is going to be a restful resource. And since we're here, let's go ahead and create a request object as well, one for storing the tickets. This is going to be in reference to the store method that we are going to be using. So StoreTicketRequest, and we will have those two classes already available to us. So inside of our application, let's start with our model because we do need to define what can be mass assigned. So we will start by creating that protected $fillable class variable. And as far as the fields email, title, content, anything that's coming from the user, and really slug as well. Because whenever we create a new ticket object, we want to also just go ahead and create that slug as well. And that should be it. There's already going to be a default value in the database for the status, that is going to be pending. And the user id is also null, so were fine there. So next, let's go to Http > Requests. And let's write or at least finish this request object. So the authorize method is going to return true because anybody can create a ticket. And as far as our rules, we definitely want the email address, so let's make that required. Let's also say that it needs to be an email address. And while I'm thinking about it, we also need to perform some client side validation as well. Something that would give the user immediate feedback. So probably in the next lesson, that's what we will tackle. And then we will have content. Those are the three pieces of information coming from the user, so they are all required. And of course, the email address has to be an email. So we are going to refer to this inside of our controller. So I'm just going to copy that, so that I can be lazy and paste it in here. We can use App\Http\Requests and then our StoreTicketRequest. So there that is. Now as far as the methods are concerned, we created a resourceController. But there are some of these methods that we just won't need, like create. This is a method for showing the view for creating whatever resource that we are creating. Well, we aren't going to be showing any views, so we can get rid of create, we could also say the same thing for edit. And that means whenever we set up our routes, we're going to use a special method that won't set up the routes for those types of methods, like create and edit. But we do want to implement this store method. So the store method is going to accept this StoreTicketRequest. And inside of this method, we are going to create a new ticket. So let's use App\Ticket, because we need a reference here, and then we will just fill it with the data that we want. So our variable will just be tickets. We will new up the Ticket constructor. And as far as the title is concerned, we are going to get that from the request. In fact, we're going to get everything from the request except for the slug. So here we will say request and title, and we will do the same thing for the email and the content. But when it comes time to creating the slug, what we're going to do is make an MD5 hash of a unique value. So the code will look like this. You'll just call md5, and then we will call iniqid, and that is going to be our slug. That should be sufficiently unique, I would think. And then we will simply save the ticket. Now, the final piece is a our routing. So let's go to the routes folder, let's go to api.php. And here we are going to use our Route and then we're going to use a method called apiResource. Now, we could just use resource if we wanted to, but this is an apiResource. There are some things that we just don't need, like the create and the edit methods. So that is what apiResource is going to do. It's going to omit those unnecessary endpoints. And really this should be tickets, not just ticket. And then we want TicketController. So all of our pieces should be set up and ready to go. So whenever we go to the browser, let's refresh this page and let's essentially do the same thing. The data itself doesn’t necessarily matter. I mean it does, but I just want something to test. And so whenever we submit this, hopefully we will not get a 404, we don’t. But instead we get a 500, so let’s look at the data that came back from the request, and nothing there. So let's go to the Network tab. Let's clear everything. Let's submit. And then whenever we see what the message is, Class TicketController does not exist. Of course it doesn't. That should've been TicketsController, so there we go. Let's go back. Let's resubmit this, and we get a 200. So now we just need to actually see if we have something in the database. So let's go to our Tickets table and let's select the first 1,000 rows and there we go. We stored our first ticket, which really isn't a ticket but we have some data. And so in the next lesson, we need to add client side validation to our form. And that's easy enough to do. We will pull in a package that will essentially do all of the work for us.

Back to the top