Planning RESTful Routes 0:00It's always fun when you learn a new framework to figure out the data side. So, you learn routing, you learn Vues, you learn some controllers, but then you get to move on to the fun part. So, how do you work with your database, and what does that workflow look like? Well, we've hit that lesson. So, let's imagine... I'm going to switch to my routes file. Let's say you're adding a section to your site for something like note cards. So, we have this concept of a card, and each card consists of various notes within it. How would we represent that? Well, let's see. Maybe the user visits example.com slash cards, and we want that URL to display every card that we've created. Now, later, I'll show you how to limit that to only, for example, the authenticated users card. But right now, we'll just fetch all cards. Now, I want a controller for this. So, we'll have a cardsI'll show you how to limit that to only, for example, the authenticated users card. But right now, we'll just fetch all cards. Now, I want a controller for this. So, we'll have a cards controller, and specifically, a general RESTful pattern to get into is to name your methods according to the resource type. So, in this case, to show all cards, we would have an index method. To create a new card, we would call a create method. To handle the process of creating the card, well, that wouldn't be this route. This route would display the form where you fill it out, and then you submit that form. Well, where do you submit to? You would send a POST request to cards, and that would hit a method called store. Now, remember, the reality is you can name this method anything you want. I'm just referring to a common convention. Anyways, a couple more. We'll go over this in quite a bit more detail in the REST lesson. But yeah, if youname this method anything you want. I'm just referring to a common convention. Anyways, a couple more. We'll go over this in quite a bit more detail in the REST lesson. But yeah, if you wanted to show a specific lesson, you might have a URL of card slash and then some identifier, and you would call a show method. To show a form to edit that specific card, you would use a URI like this, and maybe a method called edit. Once you submit that form, you are updating the card, so you would send at that point either a PUT or a PATCH request, and that would hit a method traditionally called update. And then finally, to delete the card, following basic principles of REST, you would send a DELETE request to this URI, and that would hit a method called destroy. Okay, if that was a little overwhelming, it's okay. We'll get to all of that. Let's stick with the general GET request to display all of our cards. Creating Controller and Views 2:14Okay, if that was a little overwhelming, it's okay. We'll get to all of that. Let's stick with the general GET request to display all of our cards. I'm going to switch to the terminal and run php artisan make controller cards controller. Now, that'll create a directory with an app, HTTP controllers, and specifically, I need a method called index. Okay, I'm going to hide this sidebar. Now, we can get rid of this section right here. So, I'm going to display a view, and you could call this cards, but generally, if you have multiple resources, you can separate them into their own folders. So, if a card is a resource, then within our views directory, we could have a cards folder, and then to display the index action, we could do cards.index. That means we would need to go into our resources directory, views, create a new directory within there, so resources,and then to display the index action, we could do cards.index. That means we would need to go into our resources directory, views, create a new directory within there, so resources, views, cards, and then within it, an index.blade.php file. Now, in the previous lesson, or one of the previous videos, I taught you about layout files. This is really useful. We define our wrapping boilerplate in one location. So, now here, I can just say, this extends our layout file. Next, our layout has a yield for content, so that's a good place to define my main section here. Why don't we create an H1 and just say, all cards, and then we'll view this in the browser. php artisan serve, and open that up. Now, if you're working along, you're going to hit this page, but remember, that's only because I deleted the home route. We're going to go to cards instead, and there we go. Alright, Configuring SQLite Database 3:50if you're working along, you're going to hit this page, but remember, that's only because I deleted the home route. We're going to go to cards instead, and there we go. Alright, so presumably, we want to have some cards to fetch from a database. How does that work? I'm going to show you two different ways. We can use Laravel's query builder, or we can use, and more likely, we can use Laravel's ORM called Eloquent, and trust me, you'll love these. If I switch back to my editor, we can go into our config directory, and this is where all configuration for everything from your views to your cache mechanism and your database. You can configure any and all of this. Alright, so let's go to database, and here we can see our default database connection. We'll use MySQL. Now, if we scroll down, here's a list of each of the connections, so you can select SQLite, MySQL,database, and here we can see our default database connection. We'll use MySQL. Now, if we scroll down, here's a list of each of the connections, so you can select SQLite, MySQL, Postgres, SQL Server, and you can even create your own down the line if you need to, but generally, you'll never have to do that. Now, we're going to use SQLite because it's file-based, very easy to set up. For little projects, it works great. For anything large with a lot of traffic, you'll probably want to upgrade to MySQL or Postgres, but yeah, for little things that don't get too many hits a day, SQLite is great and easy to set up. Either way though, the wonderful thing about Laravel is you just swap it out and you're good to go. So, I will change the default to SQLite here. Remember, this corresponds to this. Now, like I said, SQLite is file-based, so it's going to look for its file database within ourSo, I will change the default to SQLite here. Remember, this corresponds to this. Now, like I said, SQLite is file-based, so it's going to look for its file database within our database path followed by database.sqlite. That just means look in the database folder for a file called database.sqlite. So, let's create that. You can manually do it or from the command line, you could say touch, database, and then that file. Now, you'll see it show up. Now, when it comes to setting up your tables, in the past, you might have manually done this. So, you open up maybe a GUI like SQL Pro, and you hit the button to create a new table, and you start filling out the columns. For personal stuff, again, that's okay, but what about when you're working with another teammate? What if you change a column's type? Well, how does your team member know that? Are they going to have to manually do it as well? There's a lot of potential concerns here. Building Migrations and Tables 6:05another teammate? What if you change a column's type? Well, how does your team member know that? Are they going to have to manually do it as well? There's a lot of potential concerns here. So, Laravel uses what we call migrations. It's a nice PHP way to define the schema for a table, and then you only need to run a simple command to run all of your migrations. It's pretty great. So, if we go into our migrations folder, out of the box, Laravel includes two migrations. One for a password resets table, and another for a generic users table. And these are included by default because you will almost always have a users table. So, this saves you some work. And notice how easy it is. We define how a migration should execute, and then we also define, well, if you needed to roll that back, think of that as, like, an undo. If you needed to undo all of this, what would that look like? And in this case, we drop theand then we also define, well, if you needed to roll that back, think of that as, like, an undo. If you needed to undo all of this, what would that look like? And in this case, we drop the users table. Now, in our case, we don't have this concept of users, so I will delete those for the time being. Now, what about creating our own migrations? Again, we can use a generator for this. php artisan, and if we scroll down, we can see we have a make migration command. Now, these are all new to you, so make sure you run help, and then the command name, to figure out what you can pass. In this case, we give it one argument, the name of the migration, and then you can alternatively add a few options here, and that will just modify the boilerplate stub that is used to build up the file. In our case, the important one here is create. So, let's try this. php artisan, make migration, and the general convention you're going to usestub that is used to build up the file. In our case, the important one here is create. So, let's try this. php artisan, make migration, and the general convention you're going to use is just describe what you're doing. So, if you are creating a cards table, name it that way, create cards table. If your migration is to add a username to a users table, then name it add username to users table. That's a good and simple convention to get into. In our case, create cards table is great, and then the table we're creating is called cards, and again, that's just going to manipulate the boilerplate that Laravel uses to build up the file. I'll show you in a minute. All right, that's done. So, if I switch back, we now see our migration with some boilerplate, and because we specified this option here, Laravel knew to use this bit of code here versus some different code to modify an existing table. Okay, let'sour migration with some boilerplate, and because we specified this option here, Laravel knew to use this bit of code here versus some different code to modify an existing table. Okay, let's hide the sidebar. Now, what does a card consist of? Pretty simple. Maybe a card has a title, and that'll be a simple string. So, I can say table, I want a string column for the title. Now, in real life, a card might belong to a user, so you might do something like table integer, user ID, and you can add unsigned index to that if you want. We want an index to the column, and this should be positive. Anyways, to start, I think we're going to keep it like that. Now, also notice this timestamp section. This is a good habit to get into. When in doubt, keep it on. This declares that we want a created at and an updated at column stored for the table. Now, what about down? Well, once again, Laravel did this for us. So, if we are building up a table Query Builder with Tinker 9:12keep it on. This declares that we want a created at and an updated at column stored for the table. Now, what about down? Well, once again, Laravel did this for us. So, if we are building up a table for this migration, to undo that, we would drop the table completely. All right, let's go ahead and migrate our database. php artisan migrate. And we are done. Great. So now, what if we want to play around with our code? I'll show you a cool command, php artisan tinker. Whenever you need to try something out or see how a command or an operation works, this is a good command to run. You can do simple things like this, echo foo, you get the idea. However, we can also work with Laravel. For example, we're going to use Laravel's query builder to create a new card. We can say db table, we called it cards, and I will insert a new row, where the title is my new card. And now, if we use another tool called eloquent, it will automatically set the timestamps for us.table, we called it cards, and I will insert a new row, where the title is my new card. And now, if we use another tool called eloquent, it will automatically set the timestamps for us. But until then, let's do it manually. Created at will be a new date time, and updated at will be the same thing. Okay, so let's run that. And it's done. So now, if I were to say db table cards, get me everything from it, you can see we have exactly one row there. Now, we can also do things like cards, where the title is equal to my new card, give me the first results. Now, in our case, we only have one record, but we'll still get that. And we can even delete it. So we can say db table, give me any row that has a title of my new card and delete it. And now it's gone. So if I hit up, and by the way, I just hit Command K to clear the screen. But if I hit up, I can cycle through my previous commands. So now if we fetch all of our cards, there's nothing in there. So I will hitup, and by the way, I just hit Command K to clear the screen. But if I hit up, I can cycle through my previous commands. So now if we fetch all of our cards, there's nothing in there. So I will hit up a couple times and reinsert that last one. And now if we run it, we can see it here. So let's do one more. And we'll say my second new card. Now we have two records in there. Let's now fetch these and display them on the page. So we'll close all of this out. And in our cards controller. Now remember, in a moment, I'll show you how to use Laravel's ORM called eloquent. But until then, this is what you would do. cards equals db table cards get. Next, we have to pass that to the view. And you've already learned this. So in our case, we only have one variable, I can just say compact cards. Now let's go to cards slash index. And we're going to filter through them using blade, I can say for each cards as card, and then call in for each at the bottom. And now here, let'scards. Now let's go to cards slash index. And we're going to filter through them using blade, I can say for each cards as card, and then call in for each at the bottom. And now here, let's use a div. And we'll simply echo out the cards title. Alright, so if we come back to Chrome and give this a refresh, what's going on here? So it looks like it can't find this class DB. Why is that? Well, here's why. If you're not familiar with namespaces, we'll go over that in a future lesson, maybe in one of the follow ups. But until then, just think of a namespace as like a folder structure. So if you are organizing all of your music, you might create a folder called music. And then you might have a folder within it called pop. And within it, you might have a subfolder called Coldplay. So you have a hierarchy of folders. And the advantage to this is, if you have some specific Coldplay song, that is the exact same name as a different song by somebody else,called Coldplay. So you have a hierarchy of folders. And the advantage to this is, if you have some specific Coldplay song, that is the exact same name as a different song by somebody else, you don't get any overlap, because they're all stored in different directories. Namespacing is kind of like this. So we're saying this class is really app HTTP controllers, cards controller. So now when we reference DB, it will assume we're in the same namespace. So it's trying to find this and that doesn't exist. What we need is a DB class, not an app HTTP controllers DB. Here's how we fix that. The quick and dirty route is to proceed it with a backslash. And that means nope, not this namespace. We're starting from the root, the global root. So now if I were to come back and get that a refresh, it works. And alternatively, if you want to import this, which is what I generally like to do, I would keep that like it is. And then here I wouldif I were to come back and get that a refresh, it works. And alternatively, if you want to import this, which is what I generally like to do, I would keep that like it is. And then here I would say use DB. So now if you are curious about all of this, it's the exact same thing. In this case, we pull in illuminate HTTP request. Because if you did not have that, and you referenced the request object, well, again, it would think you were referencing app HTTP controllers request. And that's not what we want. We want illuminate HTTP request. So we import this at the top. And then we can reference that request class exactly like this. It's the same thing. Okay, quick refresher. If you already knew that, thanks for sticking around. Anyways, if we come back, we can see these here. Now what about this second approach called eloquent? Well, it's very similar. Laravel uses what we call the active record pattern. And that's just a Using Eloquent and Model Binding 14:23we can see these here. Now what about this second approach called eloquent? Well, it's very similar. Laravel uses what we call the active record pattern. And that's just a convenient way to represent a record in the database as a class. And we'll talk about that more in the future. But now how do we how do we use eloquent? How do we opt in for that? Let me show you. If we close this out with Ctrl C, I can say PHP artisan make a model. And now we want the model name to correspond to the table in some way. So if the table is called cards, a good name for the model is card. And that way, each instance of card will represent one record in that table. So let's run that. And now you'll see that by default stored within your app directory right here. Now once again, notice the namespace. And notice how our namespace corresponds to the directory structure. So this is in the app directory. So the namespace isdirectory right here. Now once again, notice the namespace. And notice how our namespace corresponds to the directory structure. So this is in the app directory. So the namespace is called app. But if we go into HTTP cards controller, well, now we're in the app HTTP directory. So the namespace corresponds to that. Okay, so we want to make use of card, it looks like we need to import that. So right here, I could either say, app card and give me all of them. Or once again, we could say use app card at the top. And then I only have to reference the class name down here. Give me all cards. And that's it. So if I come back to Chrome, and we give it a refresh, it works. And typically, yeah, rather than using the query builder, that's good for some cases. But most often, you'll find yourself using eloquent here. And it's really great. So let's do one more. And then we'll call it a day and talk more in the next lesson. I want to show a specificBut most often, you'll find yourself using eloquent here. And it's really great. So let's do one more. And then we'll call it a day and talk more in the next lesson. I want to show a specific card. So right now, yeah, we have a route to display every single card. But maybe when you click on one of these, it narrows it down. And you can see more information about that card, maybe even all the notes associated with the card. So maybe we can say cards slash an identifier. And that will take me to one card. Alright, let's set that up. Back to my routes file. I'm going to duplicate this and say, when we visit card slash, and then an identifier, well, how do we represent that? I don't want one because it's a wildcard, it can be anything in Laravel, surround it with curly braces and give it a name, whether it's ID or card. Think of that as a regular expression. We don't care what it is, we just want to match it.in Laravel, surround it with curly braces and give it a name, whether it's ID or card. Think of that as a regular expression. We don't care what it is, we just want to match it. Now we're going to load the show method on the cards controller. This will accept the card. And if we just return it directly, let's take a look at what we get. Go back to Chrome, visit the card with an ID of one. Now real quick, let's go back to PHP artisan tinker to ensure we're on the same page. Now once again, I could use the query builder to fetch all of our cards. Or I could say app card all to fetch them. Anyways, notice how each one has an identifier, two or three. This is the primary key. It's a unique identifier that refers to this specific record, it will always be unique. So if this one is two, if we visit card slash two, notice we get the number two. So we did respond to that route. Now to fetch the actual recordrecord, it will always be unique. So if this one is two, if we visit card slash two, notice we get the number two. So we did respond to that route. Now to fetch the actual record from the database, I'm going to show you two different ways. And you're going to love the second way. But the first way is okay, well, we have the identifier. So I will call this ID. And I will say, well, the card will be card find by the ID. This find method expects the ID of the row you want. Alright, so now I'm going to return the card. And if we come back and refresh, we can see the record here. And now a quick note about this, when you return a record straight from your controller, and you don't load a view, Laravel is smart enough to automatically convert that to JSON for you. And that makes Laravel really appealing for building APIs and such. So if we switch back, yep, we could load a view, return view, cards dot show this time.that to JSON for you. And that makes Laravel really appealing for building APIs and such. So if we switch back, yep, we could load a view, return view, cards dot show this time. And once again, pass through the card. Next, I will create that resources, views, cards showed up late that PHP, this view will extend our main layout. And then I'll say for our main content section, I want to display within an h1, the title of the card, and then presumably lots of more information. Okay, so I'm going to go back to Chrome, give this a refresh. And now we're viewing that specific card. So slash cards gives us all cards. And then when you click on a specific card, we'll add a link later. But when you visit a specific card, we fetch the associated row. So here's the card with an ID of three. And now we can see that record. And you can imagine this exact same workflow for building a blog or something like that. Okay, so to finish up,row. So here's the card with an ID of three. And now we can see that record. And you can imagine this exact same workflow for building a blog or something like that. Okay, so to finish up, that was the first approach. I also told you there was a second approach to Laravel offers what we call route model binding. And that's kind of a fancy term that just means something cool. If I go to my routes file, what we are really saying here is I want to find a specific card. So behind the scenes, Laravel can do the work of tracking down that specific model in that record for you automatically. All you have to do is type hint it. So if I say card, and if you're not familiar with type hinting, think of this as a way of hinting what kind of instance you expect. So I expect an instance of card to be passed into this method. But anyways, notice the difference. I'm going to comment this out. And actually, before we do that, once again, return the card,expect an instance of card to be passed into this method. But anyways, notice the difference. I'm going to comment this out. And actually, before we do that, once again, return the card, refresh, we get three. But if I now type hint it, that's our way of telling Laravel, ah, no, we actually want an instance of card. So if you don't mind behind the scenes, go ahead and fetch the record with an ID of three for me. So if I give it a refresh, now it works. We have that full card instance. Now I will give you one quick caveat about this. In order for this to work, you have to type hint it. But also you need to make sure that the wildcard name matches up as well. So if I were to change this to foobar, this is not going to work. And to prove it to you, give it a refresh. Nope. We need to make sure that the wildcard matches up with the variable name. So in this case, if our wildcard is called foobar,And to prove it to you, give it a refresh. Nope. We need to make sure that the wildcard matches up with the variable name. So in this case, if our wildcard is called foobar, then make sure that the variable is called foobar as well. And now that would work. Refresh. And there you go. Okay, just a little thing you need to be aware of. But generally, as a good rule of thumb, you would name this card anyways. So if we come back, we can change this. And Laravel will do the work of finding the record for us. Now that means I can remove that entirely. Uncomment this. And if we switch back, we're going to get the exact same thing as we did before. But yeah, less code to write. This is one of the great things about Laravel. It takes those things that you have to do over and over, and it simplifies it drastically. Okay, I'll see you in the next video.Laravel. It takes those things that you have to do over and over, and it simplifies it drastically. Okay, I'll see you in the next video.