Creating the View Form 0:00Hopefully, you are learning a lot in this series. We already covered quite a bit. We have our resourceful routing for a user's collection, and then we also have a dedicated controller for our users. But now, how do we start going about modifying records within the database? Well, to do that, we need to learn more about form handling. We've learned that this should display a form to create a new user. Let's do that now. I will load a view called users.create. Next, I will create that at views users.create.blade.php.I will load a view called users.create. Next, I will create that at views users.create.blade.php. And once again, I can just save myself some time and copy a bit of this over. Now I can just remove all of that. Now to create a new user, I can say create new user here. And let's view this in the browser just to make sure everything's working. If I load this in the browser with our server running with phpArtisanServe, there we go. It is working. All right, so we can take advantage of Laravel's form class for these things. For example, to open a new form, I can say form open and then subsequently form close. Using Laravel Form Helpers 0:57All right, so we can take advantage of Laravel's form class for these things. For example, to open a new form, I can say form open and then subsequently form close. So if I save that and I come back and I view the source, you can see it takes care of setting the method, what the action will be. Those defaults will always be post and the current URI, by the way. However, it will also append a hidden unique token for some added security. Next, let's add some inputs. If I switch over to SQL Pro, we need a username and password to create the new user. So we could say form label for the username. And why don't we give this label's text username, like so.So we could say form label for the username. And why don't we give this label's text username, like so. Next, we need to create our input. Form input. This will be a text input and the name will be username, like so. Now if I come back to the browser and once again view the source, notice that it handles a lot of that boilerplate stuff. For example, it sets the for attribute. It gives the input a name and ID equal to what you specified there. So it just cleans things up a bit.It gives the input a name and ID equal to what you specified there. So it just cleans things up a bit. However, because it's so common to create a text field, we can also use form text and simply omit that first argument. That will achieve the exact same result, like so. So let's do this. Why don't we wrap these within a div just to have some free formatting. And then we will repeat this to do one more for the password. Label for password, like so. And then this time, rather than doing form text, well, we could do form input with aLabel for password, like so. And then this time, rather than doing form text, well, we could do form input with a password type, like so. However, once again, we can just say form password and then provide a name. Now if I come back and reload, here's our username and here's our password field. Finally, we just need to add our submit button. Form submit. And once again, if we need to override the default text, we can do it as the first argument. Otherwise it will simply default to a button text of submit. Here we will say create user. Exploring Field Options 2:55Otherwise it will simply default to a button text of submit. Here we will say create user. Now if I come back, there we go. We have our form that will post to the current URI. And in this case, we simply aren't responding to that. Now as you might imagine, pretty much any kind of form field that you could require is available. Simply refer to the Laravel documentation to see what the syntax is. So for example, if we instead want this to be a checkbox, now that's going to work. Also, if we need to set default values, we could say John Doe, like so, as the secondSo for example, if we instead want this to be a checkbox, now that's going to work. Also, if we need to set default values, we could say John Doe, like so, as the second argument, and now that will be populated. But likely, you wouldn't want to hard code that in. Instead, you would reference something like user, username. And that would be good for editing a page. But you know what? Laravel makes it even easier using something called form model binding. However, we aren't to that point just yet. So let's keep it really simple. Setting Form Action Routes 3:47However, we aren't to that point just yet. So let's keep it really simple. Now in this case, like I said, the form is going to post to the current URI. But that's not really a restful way to go about this. Instead, I want it to post to the users collection. So I can override that as the first argument to the open method. In this case, I have two choices. One, I could hard code the URL. So we could do users, like so. Now if I come back, reload, the action has been updated.So we could do users, like so. Now if I come back, reload, the action has been updated. Or, because we are using resourceful routing, we can take advantage of what we call named routes. Quickly, I will run phpArtisan routes. And notice that because we used route resource, each of them received a name. And think of named routes as a way for us to reference a name and have that map to a particular route. However, what's convenient about them is, if the URI that you registered changes for some reason, you can still reference that name and it will automatically update to reflectHowever, what's convenient about them is, if the URI that you registered changes for some reason, you can still reference that name and it will automatically update to reflect the changes. If you did not use named routing, and you hard coded in the URI, well, like I said, if in the future you need to change that, well, you'd have to edit all of your views and update any of those references. So in this case, to create a new user, we want to reference users.store. So I could say, route is users.store. Now if I come back and reload, we will get the exact same thing. So let's try this out. Adding Store Controller Method 5:12Now if I come back and reload, we will get the exact same thing. So let's try this out. Let's click create user, and now the route is registered, but we don't have a method for that. So let's do that now. I will switch back to my users controller, and right here at the bottom, we will create that new method. Method, store, and why don't we just say, return, create the new user, given the post data. Alright, let's go back, try it one more time, enter some gibberish here, click create user,data. Alright, let's go back, try it one more time, enter some gibberish here, click create user, and now we have hit that method. So in the next lesson, we will learn how to fetch that data from the user and create a new record in the database.