Databases & Migrations | Laravel 8 For Beginners | Learn Laravel 8

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys my name is dari and i hope that you have a wonderful day what we've done so far was a lot of static stuff which is all right but that's not the reason why you should use laravel our main goal is to basically put data inside our database pull it in the code and do something with it and for that laravel comes with a very very cool tool called eloquent besides the power of eloquent it's also one of the most important features when working with databases and in my opinion this is where you see the actual difference with php or any other php framework eloquent is an orm our ram stands for object relational mapper this might sound pretty scary or difficult but you will get used to it very soon orm basically allows you to write queries using the object oriented paradigm so not in a programming language but more using laravel's language of choice instead of sql before we hop right into writing queries and other stuff i want to show you how the database credentials are stored in our root directory we have a folder called convict so let's click on it and right here you see a lot of files that we might need later on but for now let's just open the database.php file we also need to open our emv file so let's do that as well right here you can find your database variables that are referring to the variables inside the database.php file as you could see our default refers to our environment variable called db connection and it's mysql so if you want to change it or use another database provider change it right here but we will be using mysql for this course so let's keep it like this there are lots of other drivers that you could use we got sqlite right here we have mysql of course we have pg sql srv which i actually never worked with but keep in mind that you could also edit or delete these named connections or create your own a good thing to know is that you could also have more than one connection you can make up to five database connections and there are a few reasons why let's say that you have multiple types of data right or you might reach from one and right to another that's when it's very handy to use two drivers so let's create our own connection in our emv file let's fill in the fields that we have like i said our database driver is mysql our database host is well for me it has to be my localhost so play around with it the database ports are right the database name well we haven't created yet but we will do it in a second the database username is root and my database password is dari1234 if you don't have a password just keep it empty all right what we need to do now is to hop to the terminal and right here let's go into my sql and to do that let's write down mysql dash u it will ask for a password and my password is dari one two three four like i just said let's hit enter and right now we're in our mysql so what we need to do first is to create our database right so let's say create database called first project let's hit enter and you can see that one row has been affected now to see if it works let's write down show databases and as you can see right here first project has been created and the way i prefer to work is to not close off my sql but just to open a new tab and let's go right into our desktop workspace for his projects so we don't need to fill in our password every single time we want to go into mysql you might think that it's very difficult to set up a table with the right data types and stuff in the command line which you probably have done with phpmyadmin databases in laravel makes it a lot easier as well what we need is something called a migration which is a new term in this course and you can see a migration as the version control for your database so how in heck do we create a migration you can run it right inside of the command line but before we do that we need to set up our controller and our model so first let's create our controller so let's say php artisan make me a controller called posts controller now what we need to do is to go to our controller let's close off the emv and database file because we don't need it all right let's open the app http controllers host controller and here let's create a new function called public function index what we want to do is to return a view with a path to post forward slash index all right let's create that specific view now all right so let's open resources views let's create a new folder called posts let's create a new file in there called index.blade.php and let's write down posts for a second what's next well we need to create our route so let's open routes web.php and i hope you're getting a little bit more familiar with it so let's add a new use well we could actually copy it because it's in the same directory and let's change the pages controller to posts controller now let's create our endpoint so posts endpoints which has a route that request to forward slash posts comma pass in the post controller class comma and the method is index all right let's save it let's save my in excel blade as well and my post controller to be sure alright that's up to google chrome let's change the endpoint to forward slash posts and post has been printed out alright are we done or do we need something else well if you're familiar with mvc we need to have a model right in order to interact with the database so let's create a model as well let's hop to item again let's write down php artisan make me a model called post and it needs to be singular let's hit enter our model has been created successfully and like i've said before models are stored right inside of our app directory but instead of going to controllers we have a folder called models so let's open post model what we want to do right now is to create tables and therefore we need to add migrations now there are two ways how we could do this the way i don't prefer is to use the migration command so let's hop to i term and inside our terminal is to basically say php artisan make me a migration called create posts table so we will create a migration with the name create post stable and honestly this is not the best way to create a migration what you want to do is to automatically create one when we create our model because that's where a model is used for right to interact with our migrations so let's hop to visual studio code and let's delete our post model we don't need it let's hop back to i term and let's get rid of the command as well let me zoom in all right and in here let's create our new model and our migration so let's say php artisan make me a model called post space and let's add a dash m flag which will automatically create a migration for us let's run it alright you can see that the migration and model have been created successfully now your next question might be well dari where are my migrations stored so let's open visual studio code let me actually close off the routes and the views because we don't need it and as you can see we have a database folder so let's open it and you can see your migrations folder so let's click on it and let me actually make this a little bit bigger because you can see that we have more than one migration and we only have created one well laravel comes with the default migration of a login system on the register system create password password preset and so on this is something that we will cover later on now what we do see is the create posts table right here so let's open it and let me make it smaller again because we don't need the rest right here you see our migration well we have a up method and a down method now how this works is whenever you run the migrate command it will look for the earliest date of your migration as you could see right here and it will run the up method like it says you're migrating it up at this point and a very very cool thing is that you could also roll back your migration so you don't need to delete it inside your my sql manually but you could do it inside the down method which you will see because it has a drop if exists and it will drop the table posts little recap the up method migrates everything inside of it and the down method will undo everything that has been migrated right now we're actually ready to create our first table whether in a create table call or a modify table call it will always use the instance of blueprint that's passed into the closure of the schema if we click true on blueprint you can see all the various methods available for creating columns and you don't need to learn them right now because it will come during this course you can create integers string binaries you can build stuff you can add fluid indexes and way more alright we're ready to create our first table let me open my migration again and what we don't need is our id and timestamp because i want to create everything from scratch so let's delete it what do we need when you want to create a post well we have our table variable which has a method called increments init now inside the parentheses we need to write down the table name so in our case it's id we could pass in an integer right here instead of increments then we need to manually increase it every single time so why should we not use our increment method that we have so what's next well every post has a tape every post has a title so what's next well every post has its own title so what we could do is to say table which is a string and the name is title so the column name is title the data type is string we have a body so table medium text and let's give it a name of body let's also create a timestamp to know when it has been created so let's say table timestamps once you have your migration defined which i have right now we are actually ready to run it it will not change it automatically when you save the file like i'm doing right now because we need to run a very easy command let's hop to i term and let's write down php artisan migrate just as easy as that let's hit enter and you can see that it calls the up method of all the migrations that we had every single time you run this specific command it will check whether you're ready to run all available migrations so if there are no typos or whatsoever in your up methods and as you can see it has migrated the post table that we just created so let's hop to mysql in here let's say show tables oh let's first say use first project excuse me all right then let's say show tables and you can see that we have a users table post table that we need password reset migrations and failed jobs don't look at the others for now what we need to do is to say desk so descending our table posts let's hit enter all right and you can see that we have a column id title body created at and updated at now there are a couple other commands related to migrations that i would like to show you the first migration is well let me go to another tab php artisan migrate colon install and what this will do is keep track of which migration you have or haven't run let's hit enter right now it won't work because it's pretty much the same as the php artisan migrate command because it will be run automatically behind the scenes when we migrate the second command is the migrate column reset which we'll call the down method inside our migration so if you run it and go to mysql try to desk our posts again you can see that our table post does not exist anymore because we have rolled it back we also have php artisan migrate refresh and this will roll back every database migration you've run on this instance and then runs every migration available and this is basically the same as the reset but it will also migrate it so right now if you hop to my sql and say deskpost again you can see that it reseted it and then it runs our migrations again there are three more commands but honestly we won't be using them a lot but i will just tell you what they do but i won't perform the commands we have the php artisan migrate fresh which will drop all of your tables and run every migration available it's the same as a refresh but this won't use a down migration it will just delete the tables and then runs the up migration again we have the rollback which will well basically roll it back and the last one is the status and we could actually run this one because it will just show a table listing of every migration whether or not it has been run yet in this environment this was it for this video about databases and migrations if you do like my videos and you want to see more or you just want to support the channel just click on the subscribe button down below and don't forget to like this video
Info
Channel: Code With Dary
Views: 8,478
Rating: undefined out of 5
Keywords: laravel, laravel 8, laravel php framework tutorial - full course for beginners 2020, laravel 8 tutorial for beginners, laravel php framework tutorial full course for beginners, learn laravel for beginners, learn laravel step by step, laravel full course, php laravel youtube, laravel tutorial youtube, how to learn laravel, laravel tutorial 2020 - the complete developer course, laravel tutorials from scratch to advanced, databases laravel, migrations laravel, how to use databases
Id: g-63ClKANsM
Channel Id: undefined
Length: 15min 8sec (908 seconds)
Published: Wed Dec 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.