Laravel 8 - Migration

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys welcome to live tips so in today's video we'll be talking about migrations so what do you mean by migration so migrations are simply the version control for your database so for your file the version control is git so for the database the version control is migration so with migration what you can do is you can create a database you can create tables columns remove the driver remove the tables columns and also roll back to the previous state so basically it is a version control for your database also there is one benefit for using migrations that if a user creates a migration in a project then he can share that migration and the user in the other end can easily download the code and run the migrations and both the users will have the same database tables so without any further delay let's begin so first of all let us look at the laravels configuration file and how the migrations and database connections are set up so we'll first looking at how to connect to the database and then we'll be looking at the creation of the migrations yeah so i have opened my previous project so if we look in the project structure and if we go to config and database we'll be seeing here a lot of options okay so by default laravel will be using mysql what we can choose you are not forced to use mysql you can use other options as well like you can use sqlite yeah and you can use pg sql sql srb yeah so these are the available options but we will be using mysql yeah so how to connect a database so whenever laravel tries to connect to our database then it looks into this default configuration dv connection and this value it is taken from the environment variable which is inside the dot enb file which is in the root of our project this is dot enb file and if we look at the db connection so here it is also a mysql and it is a mysql so it will go into this array inside this connections and all these some of the options are already default values and some we can configure so for example database url db host port everything can be configured so which can be changed from here okay so for now what laravel is doing is doing a default databases laravel username as root and password as this okay so now let us try to create a table also let us see by default there are three or two maybe three three tables migration files that are provided us by laravels which is the users table which can be used for the authentication purposes like login register yeah and password resets table as well as to if a user forgets then we can use this one and also fails off so this is a little bit advanced topic so we'll be talking about the jobs in our future lessons now let's leave it as it is now and let us create other migrations okay now let us create there is a command for in the laravel psv audition command to make the migration so php partition make migration so let us create uh categories migration okay so there is one convention that we it is not a must to follow but if we follow that then every hour every migration file will look same and it will be easy to debug okay so when we are creating a migration then we will do create underscore and now we are creating a categories table okay categories create categories table okay the name of the table will be in the middle and in the front it will be category and in the end it will be table and if you hit enter a migration file will be created and which will be located here in the migration if you see here inside the database migrations and this so laravel will automatically prefix the generated migration file with the current timestamp so you can see here the current time stamp is here and then the table name is here okay and if we go click here and look here we can see categories this op method and this down method okay so when this method will get wrong i'll tell you that so basically there are two methods up and down okay and this is whenever we run php artisan migrate this method will be run okay there is a helper command in laravel so let us run that now to execute a migration we have php artisan migrate now let's see what happens if we run this look we are getting some error okay so it is saying that access denied for user root at localhost so if we look at our dot enb file then we can see here it is looking at this database laravel username root and password no but the database this laravel is not in our system okay if you look at laravel then it is not present so let us create this laravel basics database larval basics so this database is created now let us add it here okay and the root password for my machine is p as it is wrd 1 okay so this is my password now as you can see here the environment variable has been modified and it is restarting the server so this is the feature in this large elite so before the laravel 8 if we if we change anything in this dot env file then we would we have to we had to stop the server and again restart but from laravel it we don't need to do that it will automatically track the changes in the env file and automatically restart the server okay okay now if we go here and see as you can see here basically so if you are wondering what is this it is a dbwaiver community edition and so you can download it from this website dviver.ioslash download i'll be also adding this to adding this link just below that like button okay so you can download it from here so it is available for windows mac linux everything and it is completely free and we can manage our database look we look into our tables everything from here so if i click here now you can see here there is nothing no table is here now and if we see here there are four migration files okay and this is the latest one so let's see how to execute the migration psp partition migrate if we run this command then this id and timestamp fields will be available here and these four tables will also be available and this basically this id is the unsigned integer big integer and time stamps are the created and updated at time stand fields which laravel provides out of the box whenever we create any migration now let me run this migrate now you can see here it has run all those migration tables so it's running for this users table then password fails off and then categories table now if we see here refresh the and we can see here there are five tables but there are only four migrations so there is one extra table called migrations and you can see here it includes id migration and batch and if we see here click on the data then we can see here the results so these are the names of all these migration files okay so laravel uses this migrations table this migrations tables to track all of all of our migrations okay we have seen this now let's see here in the categories table now we can see here it contains id created at and updated that okay fields so this is id this is created at an updated ad and if we look here inside the properties and we can see here what are these okay id is the big integer unsigned and it is not null and auto implementing primary key then created add is a timestamp knowledgeable and updated ad is also timestamp nullable okay okay so we have created a migration and from that migration we have created a table with these three columns we can also roll back okay as i said this my migration is a version control for our database so we can also roll back so let's say i want to remove this categories table now what can i do php roll back okay so it also contains one flag called step step is equal to one okay if we run this step cost one then it will only roll back into one but let's not include this and let's see what happens okay if i hit enter sorry the command is php artisan migrate rollback and if i do this look at here all these tables have been rolled back means they all have been deleted so now if i go into this laravel basics and refresh so now you can see here all the tables are gone but only this now migrations table is remain and there is no data here okay because all the tables are rolled back let us migrate again php artisan migrate and again you can see here the data is here okay here you can see all the data now it rolled back all our tables and deleted all our tables now what we want what if you want only to delete one table okay so we can do it like this php artisan rollback minus minus s step is equals to one so now this will only roll back the latest one okay the create categories table now if i hit enter now you can see here it is only it only rolled back the categories table and if i come here and refresh the page so you can see here so there is no this categories table and all the tables are present there okay now if i again do php artisan migrate then it will only migrate this categories table all the other tables will remain same okay because it is tracking all this thing from this migration table and here you can see there is only users password resets and fields of stable only these three tables are there and this table is not there so when we run the migration it will see that this thing is not here this create categories is not here so it will only migrate this okay let's see now if i refresh here it's you can see here the create categories table row is available here and if i refresh the page then here the categories table is now present yeah okay so we can also roll back and migrate using one single command so i'll show you that okay if i say php artisan also the road sorry i would like to say one thing that if we roll back uh every data will be deleted yeah so keep that in mind so always before rolling back uh you you have to keep in mind that your data will be lost so we should be very careful before doing any rollback okay so now what we can do is php partition refresh okay if we run this command then it will roll back all the changes and it will again migrate so basically these commands are rollback and refresh are useful for local development not in the production environment okay so if i hit enter i even forgot the migration command migrate colon and if you hit here yeah you can see here so it is saying that rolling back roll back and again all those are rolled back and again it has migrated and if we see here so all the tables will be available here yeah so it does roll back and again migrate okay there is also another command to do the same thing like php addition migrate this will drop all the tables so it will not roll back it will directly drop all the tables and then it will migrate it again okay so if you if i hit enter here then you will see here okay it says dropped all the tables okay so it is not rolling back it is directly dropping all the tables and then again migrating all the tables so if you see here then everything will be here so now we have seen the migration commands we will be seeing some methods to add columns in our table okay in the categories table let's say now we need name field okay what we can do is table string name so this string is of our car by default the string is a parker and it contains 225 characters by default okay now if i say okay let's say this okay the table and let's say integer integer and active okay let's say active can be uh either one or zero so we can keep it integer or also we can keep it as ti and tiny integer yeah we can keep because one and zero will be very only two so we can keep it as tiny integer now i will show you by running the command so we can run the migrate phrase command yeah now we have only this id created at an updated ad okay now if i run the first command and if i refresh here now you can see here it has id name active id name active created and updated and if i look at the properties and if i refresh here and you can see here by default the name it is the string is giving us the varchar255 yeah and then tiny integer is the tiny integer yeah as you all know that yeah after we so after we run this migration we are getting these columns so there are a lot of helper methods like this string dynamic there are a lot of methods so we can see it in the laravel documentation if we go here columns and creating columns and you can see here all these things we can create like uh boolean chart date date time time stamp decimal you know yeah you can also create json um so many database columns are supported so you can look here and you can create your own so now i'll show you suppose let's say you don't want 255 characters you only want 100 characters then you can define it like this in the stream yeah and if i migrate face again and look in our database and if i refresh here so you can share it is only part of 100 okay okay so this is these are how you change the add the columns and now how to modify the columns okay suppose uh we created a name but it is not nullable but suppose let's say we want some of the field as nullable then what to do so we can make this active field okay currently this is not nullable as you can see here it is not null it's checked so it is not a node yeah how to make it knowledgeable we can make it knowledgeable as like this knowledgeable we can change the methods like this here and okay let's see the change and if i migrate face again come here and refresh the page and here you can see here not knowledge gone so our active column is now nullable we can also give some default value like this d for let's say zero yeah so currently here you can see here the default is nothing here yeah and if i php audition my face and refresh here then you can see here the default is zero and it is nullable as well so you can change the methods like this and add nullable default value yeah and those are also given here like modifying the columns here here you can see so not this one column modifiers yeah here you can see here so here unsigned yeah use current knowledge there is also comment yeah and also unsigned is also let's say the tiny integer it is not currently on site now if you want to make it unsigned then you can call unsigned like this or there is also one unsigned tiny integer you can call this method it will internally call that unsigned method okay okay this is how you change the columns okay after that also now we can modify the columns now we have modify the column so which is like this let's say you want to change the name yeah suppose let's say this is the name now let's say you want to change it to title now you can you you don't you cannot do this in the same file because you are creating a categories here so if you write name or title anywhere it is always created yeah and let's say you did a lot of work and there are a lot of migration files and in the future we don't want this name so all the other so many migration other micros having the name as this as a title and only this table has it as a name so we want to make thing consistent and we want to change it to the title okay so let's think about that scenario and there are a lot of migration files let's see create categories post comments reviews everything and after that you need to change this so in a production environment you cannot just go to this migration file and just modify here and change yeah so what we can do is we can create a new migration for that so what we do is previously we created a migration file which which create which created a table now we want to modify the data in the table yeah sorry column in the table so what we can do is php artisan migration create psp audition make migration now what we can write is add or we can give and name suppose let's say we want to change name to title change name to title column yeah and we can give another flag like table which table we want to modify the table name is this categories yeah so we before we didn't give anything so it created the categories so looking at this uh that i told the convention yeah like this create categories table and looked in the between and created this categories table for us now in order to modify table we can give any name so there is no any convention so we can write change name to title so if we want to add any other columns and like add name column to this table yeah so we can write this so change name to title column in categories table yeah we can end it with a table so we can follow this convention and giving a dash dash table categories will not create a and the table as categories but it will help us modifying the current category so if i hit enter so it will create another migration here yeah so as you can see here now there is something different schema table so before it was schema create yeah in the down method it was drop if exist so whenever we rolled back this down method was called and it dropped the categories okay so now there is a new migration file and this migration file is not in this migrations table yeah it is not in this migrations table okay so whenever we run migrate then only this new migration will run other will not run okay so after that what we want to do is we want to change this name to title so what we can do is here okay table string sorry not a string you can do is rename column and you can give the name so okay so from which what thing you want to change from name name to title yeah and this thing can be found in this uh renaming column section of our so and there is one thing that this enum columns cannot be renamed okay also there are only some of the database columns that type that can be changed so here is the list so you can see here in the documentation as well okay so now what we did is we changed we renamed the name to the title so whatever we do here in the op section we have to do opposite in this down section so in previously in the categories table when we created a table ladder will automatically did that for us so drop if exists the categories it will drop the categories it is creating a category table and it is dropping the categories table here so now what we are doing is here renaming renaming the name to the title so we will just opposite here renaming title to name okay so this will run whenever we run the psp audition migrate and this will run whenever we run php artisan roll back okay now let's see what happens if we run php addition migrate now you can see here there is an error okay this is this error is because we haven't installed a package so in order to perform these operations like renaming and changing anything like uh string before it was let's say 255 string name 250 we want to change it to 250 character length then we have we we have to do it like this and to perform these changes in the column label we have to download a package okay so it's a doctorine dival and i may have mispronounced it so how we can do is we can do is c-o-m-o-s-e composer install composer t-ball and hit enter so sorry not install composer require so it will install this package and after this package has been installed then we can perform those actions in our column we can easily modify our columns so the package is now installed so now let's run the same command again php artisan migrate okay now you can see here the migration has been drawn successfully and if we go here and refresh here so you can see here the migration row is here and if we go and refresh here then if you go into the categories table and you can see here it is changed to title before it was named and now it is changed to title so we can also do drop the column we can also drop the column yeah if we want to drop only one column then we can uh add it in as a string and if we want to drop multiple columns then we can add it as array so as you can see here the for asculite the dropping of uh the database is not supported yeah multiple columns so you can do these things these are a little bit advanced like this uh morph soft delete so we'll be also looking at these in our future videos not in the migrations migration part two or three like that but in a real life scenario okay in a project also after that what we can do is we can add indexes to our columns okay so how to do that so if we see here currently in our database so in the index section so there is currently only a primary key okay there is no other indexes now we want to add index to our one of our keys okay so let's see how to do that so now let's me let me roll back this now okay let us see one thing so so currently what we did was change the name to the title now let's see roll back okay psp migrate rollback step equals to one and if i hit enter here so now it has been rolled back okay now if i go ahead and see in our this column section and if i refresh here now you can see here it is converted to the name okay so this can be done now let me remove this table i don't need it yeah and i think and that is also removed in this migrations table yeah because we're on the rollback okay i have removed this now if you go here and now let us add index okay okay now index we can we can we have to just append index method here just append the index method now let us let us again go ahead and see here there are no indexes here now if i go ahead here psp artisan first so it will drop all the tables and again migrate now if i refresh here now we can see here it has created a index and it has given a name by itself okay the convention is this the table name yeah and the coulomb name and underscore index okay so the convention is like this so laravel will automatically create the name so we can also give a name here like this let's say we can give a name as let's say c a t cat name yeah if we do it like this and run this migrate face again and if i refresh here now you can see here the index name has been changed so we can also change the index name yeah so we can also make uh so suppose let's say we want to make one column unique so now we can make it unique in the database level yeah so whenever we try to store the in the database let's say we have stored previously stored there is a name called category a now if we again try to store category a in this table then it will give us error because it is a unique so let us run this again and see here if you refresh here now you can see here categories name and unique yeah so so it will give us a different index and here the unique is checked so before the unit wasn't checked and now the currently here the unique is checked okay so this is how we give index okay this is how we give indexes and now we can also add a foreign key index as well okay now let's say there is there there are categories yeah now let's say there are there are categories can have many posts yeah blockbust so let us create that table php artisan make addition create let's say blocks table okay create blocks table so it will create a new migration file you can see here so it will automatically add this create blocks and drop blocks yeah for us if we follow this convention now a block can have let's say table string as a title yeah now let's say table um [Music] long text as a body or description it is a long text in the mysql and then what it can have also is table unsigned integer as category id now this category id is a foreign key to this categories table okay now if we run it like this if we run it like this let's say php a rti sn migrate then this table will be created okay blocks table will be created now if we go here and refresh the page the blocks table is created yeah so and if we look in the column section they are all there and if we go in the foreign kit section so there is nothing so since we haven't created any foreign key so there is nothing so how to add a foreign key let's see table foreign so this is the foreign column and it references id on categories table yeah so foreign key the foreign key category id references id on categories table so you can add a foreign key like this so you can also create like on delete on delete cascade yeah or on delete set null or on you update you can do all this here so let's say on delete let's say on delete cascade yeah for now okay now let us run this migrate fresh command again and if we go ahead and refresh the page now you can see here it has created a blogs underscore this is the table name column name and now for in the all the foreign keys will have it will end with underscore foreign okay so this these are the conventions so you saw that similar convention in the indexing index in the index case as well as well as in the unique so unique in the unique it will end with underscore unique in the index it will begin with on it will end with underscore index and for foreign it will end with underscore foreign so this is a very long so what we long method of doing it so view in the previous like level up to seven we had to do this yeah currently laravel it also support the syntax so we have to define the column and again write this long syntax to make it foreign key so so by default whenever laravel creates any table they are always big integer unsigned big insert so laravel also supports has provided us a nice api of defining the foreign key yeah so what we can do is for an id yeah we can do foreign id and we can add constraint here if we do this then the foreign id so laravel will look this category id so it will look at the plural form of this okay this underscore id it will ignore this underscore id and look at the plural form of category which is categories so it will look in the categories table and it will add the index to that categories table like this yeah and after this we can call this on delete cascade here okay and if i run this again now you can see if i refresh the page so it is on the cascade here it is everything so if i refresh here so nothing has changed so all the things are same so we can do the same thing with this smaller this nice and good api nice syntax okay now sometimes we also may need to make this foreign column as nullable so for that what we have to do is we have to add this nullable before this constraint okay so now we have to add it here no level here and then only it will work otherwise it will throw an error so if i refresh the page so if i see here in the columns currently currently this is this is not knowledge checked so it is not on all okay and you can see here it has also created this big integer on site okay category id as this for an id will create this big integer unsigned and if we look in inside here you can see here if you go here inside so it will create type big integer yeah column name is the path that we have so passed so it will not be auto increment and it will be unsigned so sometimes it's good to look inside those methods so what they do inside so it will that will understand what type of columns it is using and this is using a big integer as well okay so if i run this and so you can see here it is checked here and if i refresh here okay now you can see here it is unchecked so it is now nullable so we can make a foreign key nullable by adding a prepared like appending or no level in the foreign id method so we can change this method so in most of the cases larval provides us this nice api channel api so we can change multiple methods and those will be automatically applied by the liability for us so it's a pretty nice also thank you for subscription since we have already cost 100 subscribers so as you can see here and you can see here we have already cost 100 subscribers so thank you very much for that so recently maybe three or four days ago we crossed 50 subscriber and now we are just in a span of four five days we have again gained 50 subscribers so that's all because of you that that support from you is really great thank you very much guys and i think it's about uh a month that i have started uploading the videos on youtube so you guys are awesome so thank you very much guys thank you very much also i have changed the logo so what do you think about this logo so let me know in the comment section below and this art this cover image so let me know what do you think about this image so it's nice or it's not nice it's looking good or not so i need your opinion on that so if you want to give some opinion your opinion on this then please help me out so thank you guys thank you for watching have a great day bye
Info
Channel: Laratips
Views: 2,439
Rating: undefined out of 5
Keywords: laravel for beginner, laravel beginner tutorial, laravel tutorial for beginners, laravel 8 tutorial, laravel 8, laravel 8 basics, laravel basics, migration, laravel migration, database and table creation, database version control, laratips
Id: r-k4VowOLGk
Channel Id: undefined
Length: 38min 38sec (2318 seconds)
Published: Mon Oct 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.