Laravel From Scratch [Part 5] - Models & Database Migrations

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
alright guys so this is where things are going to start to get interesting because we're going to start to work with the database and we're going to create a model a post model we're going to create a post database migration and so on so let's go to PHP myadmin now if you are if you're using xampp PHP myadmin is automatically included unless you chose not to install it but hopefully you did if you aren't using xampp and you're using a native lamp stack just go ahead and install it it's not that difficult especially if you're on a Linux distribution so let's go ahead and create a database I'm just going to call this LS app and then that's really all we have to do okay we're not going to create tables through here or have to do any of that because we have migrations so let's go back to visual studio code and I'm going to open up the terminal and hopefully you guys like this style here I think I'm actually going to really start using this in my videos this like how this terminal is integrated so easily and let's see we're going to first create a controller okay we've already done that so we know that we do PHP oops what the hell so PHP artisan and we're going to do make controller and let's say post controller alright so it was created let's look in our app folder HTTP controllers and let's refresh and there it is post controller alright now we also want to create a model so to do that just like with controllers we can say PHP artisan make model and you want to use the singular version of it okay so you want to do stu post now I also want to create a migration because we want to create a table for it for our posts so to do that we just add - M all right so let's run that and now you'll see that right here created a post dot PHP this is our model and you can see it extends model and if we look in the database folder and migrations you'll see a create post table dot PHP since we added the dash M it went ahead and created that so let's take a look at this this is this is what a migration looks like so we have a class a descriptive class extends migration and then we have an up-and-down function so when we run DB migrate it's going to go ahead and run this and as it is it's going to create a post table it's going to create an ID field which will it'll it will make it a primary key and then it's going to add to timestamp columns one will be created at one will be updated at and those will get automatically filled when we insert data through our application now you probably want to add more fields so what we would do is come over here and say table and let's see what we want we want a title right so let's do table string we're using MySQL so a string is going to be a varchar' all right so say string and we want to call it title ok so that'll actually create a title column we also want a body and I want this to be a medium text field which is longer than a string so let's do medium text and that'll be body all right and then further down that's going to happen if we roll back the migration so if we roll it back we just want to drop the entire post table all right so we're going to leave this for now later on we're going to add a user ID to the post when we implement authentication but I'm not going to do that just yet so let's save this and then if you look over here in migrations there's also this create users table and laravel includes this automatically ok so that we can easily implement authentication so users will have an ID a name an email that will have to be unique a password and some time stamps and this remember token value all right it'll also create a password reset table now there's a few things we need to do before we run the migration and create our tables we need to first go to the dot env file and input our database information so I'm using a database called LS app and my user name is going to be root and my password is going to be my root user password for my database alright so make sure yours matches yours and let's go ahead and save that and then there's one more thing we have to do before running the migration I'm not sure exactly sure why but since we have a string in here we're going to get an error that says something about maximum length and to fix that we need to go to and it's possible that you won't have this issue depending on what version you're using but I would I kept getting it so I'm going to go to I forget where as a continuity some providers providers and then app service provider and what we want to do is bring in something up here we want to use illuminate which is basically just laravel slash support slash façades slash schema ok so we want to bring in this schema library and then in this boot function here we're just going to say schema double colon and then default string length and we're going to set that to 191 alright so if if we don't include this here we're going to get some strange error when we run the migration so let's save that and then we should be all set so let's go let's open the terminal back up whoops ok and now what we're going to do is run PHP artisan migrate and now if we go to our database you'll see that there's a few tables here those users there's password resets those are the two from the two migrations that laravel includes then there's a table called migrations which actually key track of our migrations and then the post stable which we created so if we click on that and go to structure you'll see there's an ID primary key it's set to auto increment automatically there's a title a body and then two timestamps all right so our table is created and it's ready to to take some data now I want to have some initial data to work with so you could go ahead and go to insert and you can do it through here I don't want to do that I actually want to use tinker which is a tool that we can use to insert data select data and all of that so let's go back to our terminal here I'm just going to make this bigger and let's clear this out so to use tinker all we have to do is do PHP artisan tinker and you'll see that we get this these brackets right here and that lets us know that we're in this we're in the tinker program so from here we can you can interact with the database with eloquent which is an ORM that makes it really easy to do things so what we can do is we can bring in the model so let's say we're going to do so to access the model we can do app slash and then the model name so we called it Post so we can call functions with this for instance account alright and it tells us we have 0 posts in the post table and the post model so what we'll do is set a variable we're going to say variable post and we'll set that to new and then app back slash make sure you do a back slash not a forward slash and then we can do post just like that and that'll create a new instance now that instance is basically being held in memory so we can add fields to it we can say post can use that variable and we can add a title so we'll say post title we'll just call it post one alright and then we can press ENTER and we can add another another field let's do post body and we'll set that to we'll just say this is the post body enter so it has a title in a body now we can say that if we want so to do that we'll just do post save and we get true so now if we go back over to PHP myadmin and go to our post table there it is as an ID title body and the creative at and updated that gets put in automatically so let's create one more so we'll do post equals actually yeah let's do post equals new app slash post and then we'll do post title equals close to and then post body equals this is post two and then we'll do post save and there we go now we should have two records in our database all right so now we have some data to actually work with now in order to add crud functionality for our posts we need to have a bunch of functions in our controller and we need a bunch of routes all right now let's open up the control I'm just going to get out of the terminal for now whoops we shouldn't need it for a while and let's look at the post controller so we're going to need a lot of different functions here we're going to need an index okay so the index is going to be basically the listings of the listing of all the posts we're going to need and I'm just putting this in here for now you don't have to do this we're going to need a create function which is going to represent the form okay so if we go to post slash create it should be the create form and then it's going to submit to a function called store ok that's going to be the function that takes care of actually submitting it to the model to the database we're also going to need edit which is going to be the Edit form just like creatives for the ad and then that submits to a function called update which will take care of actually updating it and then finally we want a destroy which is going to take care of deleting it and actually there's one more which is show and that's going to take care of showing a single post okay so these are the main these are actually resource functions or resource methods now what we could have done which I should probably should have done is when we created our controller just clear this out oh we got to escape this I think we can do quit okay so what we should have done is PHP artisan actually we still you can still do it let's just delete the controller so I'm actually going to delete the post controller and recreate it alright so let's do PHP artisan make controller and then we're going to say - - resource up we didn't name it make controller posts controller resource and now if we go and look at the controller to get rid of this terminal you'll see that it has all of those functions that I just showed you okay and certain ones have certain parameters so index create those don't need any parameters store since we're going to we're going to submit to this from a form and we need to grab the variables from the form that's going to take in a request object all right show is going to take in an ID because we need to know which I do of the of which posts we want to show and then edit is going to take an ID for the same reason because the Edit form needs to have you know we need to know which post to show in the form update will take in requests and ID because we're submitting the form to it and we need to know which one to update and then destroy has an ID so we know which one to update so if you're if your handwriting needs and you didn't want to delete your controller and create a new one with resource just go ahead and add those functions with those parameters all right but it's fine if you just delete it and create it again all right so now we need routes to all of these so if we go to our routes file it's going to be kind of a pain in the ass to actually do to write out every single route so there's actually a shortcut we can take which is to use route resource and I don't know if I showed you this before but if we go back to the terminal clear that out you're not supposed to see that if we clear the terminal we can actually run a command an artisan command to show us what routes we have in our application so we can do that with route route list and it shows all the routes so we have slash for the home page which is a get request shows us the controller method it goes to about services and even the API user we created which is just a function or closure inside the the route now we'll go back to let's close this up and go back to our routes and we're saying route resource all right and then in here what we want to pass in is posts and we want to map it to a controller so posts controller and then it's going to actually create all the routes we need for those functions that we created so let's save this and we'll go back to our terminal clear this up and let's again do route list and look at that it created all that we need so we have C so posts get a get request to post is going to load the index you can see it actually has a name as well it's going to load the index function if we make a post request - I'm sorry if we make a get request to post create it's going to load up the create function if we make a get request to post edit down here along with the ID it's going to call the Edit function if we submit a form and make a post request to posts it'll call the store if we make a put or a patch request to post slash ID that's going to call update and if we make a delete request to post slash ID that will call the destroy so it connected all of our routes to those functions that were created all right hopefully that makes sense that it's not too confusing all right so in the next video what we'll do is in our controller we're going to bring the model in so that we can interact with it we want to fetch all of our posts in the index function and then pass it to a view that we're going to create for to view our posts all right we're going to do the same for create for edit and then we'll move on to creating our forms and submitting them to store we'll submit the edit form to update and so on alright so thanks for watching guys and I will see you in the next one
Info
Channel: Traversy Media
Views: 438,058
Rating: undefined out of 5
Keywords: laravel, laravel models, laravel database migrations, laravel mysql
Id: neSHAWdE44c
Channel Id: undefined
Length: 16min 8sec (968 seconds)
Published: Sat Jun 03 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.