Laravel Migration Tutorial - The Complete Guide of Database Migration in Laravel 9

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome to tech tool india in this video i am going to explain you about laravel migration if you're new to if you're new to the channel please do subscribe our channel tech tool india without wasting any time let's get started in this video i'm going to explain you about my laravel migration basics how we can run the migration and roll back it add or update any column in the table add a column modifier creating database indexes and creating the foreign key constraints the very first thing is about the laravel migration so what is migration basically migration is kind of version control of the database generally what we do we generally write the query in through mysql and create a table and manage those things but laravel provides a migration which is kind of manage a version of your database schema whenever you have to create a table you write a migration you need to modify some column you need to write a migration by writing the migration you are able to manage the versioning of the database so let's see how we can generate a basic migration and the structure of that migration so for this you need to install laravel 9. if you want to know how we can install this i have put a link in the description you can go and watch there assuming that you have installed a level 9 if you go to this database folder there is a folder called migrations this folder actually consists of all the migration files in our application and this is the database schema which has this default table if you have installed laravel auth package let's say i want to create a contact table so what i can do i can write a command p h p r t cent make migration and while giving a name to the migration laravel actually detects the name of the table and the column in the name of the migration if i want to create a table what i will write create underscore contacts whatever the table name will be and then underscore table so what it does it actually create a create contact table dot php file in our migration folder like this you see this is and this has been appended by the current time stamp so it actually creates a file in the database migration folder and in that folder we got the migration structure so migration structure consists of two functions one is up and second is down inside a function whatever is there it runs when you run the migration and inside the down function whatever is written it actually run when you roll back this this is a scheme of fact which accept the name of the table and this is the blueprint of the table database table consists of the columns so each column have a different data type so to understand about that we can look into the laravel migration available column types to see the available column types we need to go into the laravel documentation and we can see these are the type of column which is available which we can use table of id that id is basically it's a kind of column type which is primary key which is auto increment so the by default of table structure how we make our tables are like ids are the primary key generally obviously you can change this but if you want to use this it will be there the second is timestamp so laravel comes up with this functionality where every table consists of created add and updated add column you can obviously change it but it comes by default let's see if we want to go with the default as well what right now let's let's say i want to store a name in a context table so for adding any column what we'll do we'll use a type of string so i want to use a varchar so i'll give it as a string and name it as a name so what it does it actually add a column of name inside a contact table let's say we want to add another column which is again a type of string and name it as a mobile number so these are the two call these are the two columns the next column type we can take is status so status we can take boolean either true or false so we can take as status all right so we have added name mobile number and the status to add any other type you can use any of this column type and you can add a column in your table so this is how the structure of the migration is the next thing which i am going to tell you about how you can run a migration and roll back the migration to run a migration as we already generated i have to run a command php audition migrate as soon as i run this command what it does it actually checks in the migrations table which of the migration is not migrated it will migrate only those files let's say if i run this you see only this contacts table is migrated let's go into our database you see there is no context table earlier but when i refresh it you can see this migration and contacts table is there so we have put our id name mobile number status created at an updated ad let's see if the down function is working or not to roll back we need to run a command and the command is phprt send migrate rollback as soon as you hit this command what it does it actually roll back the last migration which it does so how the migration will work migration actually run in the batch for example if i have created three migration files at the same time and run the migration so what it does it actually migrate all those three files and give it a batch let's understand it better by going in migrations table if you see the data so migration table consists of the name of the migration file and the batch batch is actually it's a kind of version for example i have migrated it first time when and again if i write three files and migrate it so all those three files will come on the batch two so when we use this roll back it by default what it does it actually roll back the latest batch whatever the latest batch is it will roll back only those but for example if we have to roll back till last three steps right so what we can pass here in the rollback we can pass a step as well so it will roll back till the three step back the next thing which we need to cover is adding or updating any columns in database so let's go into our code first i'll run php artisan migrate so it migrates the table and now we'll have this one more migration file and the batch is two the first thing i want to do is update the name column to full name so what we can do we actually write a migration for this and rename the column for renaming a column according to the laravel documentation what we need actually for modifying any of the column we require this package dbail library so what i can do i can copy this paste it here all right so we already included this library for updating any column what we need to do now always try to write a migration in a very descriptive way like php artisan make migration and i want to update name column in contacts table so i have just written a whole statement here what it do when i hit the command it actually creates another migration file and if you see we already have this contacts table because laravel actually auto read this name of your migration and from there it actually picks this you want to update anything in contacts table so it given the contacts table i just want to rename the name to full name to rename a column what we can do we have this table and then we have a function rename column and i want to rename it from name to full name and in the down function we need to write the exactly reverse from full name to name let's save it as soon as i run this command php rt migrate you'll see it actually executed this command let's go into the database you see there is a third batch of migration run if i go this contacts you'll see this structure we have now full name the next thing i want to do is i want to give a default value on a mobile number which is null i want to make this field mobile number nullable so what i can do i can write a migration again so basically the concept is like whatever you want to do with the table you don't have to go into the mysql and update the column and everything you just need to do everything with the migration and every time you need to do a new thing you have to create a new migration file and with that it will modify your tables so what you can do basically php artisan make migration and i'll name it update mobile column in contact saver so again it created a new file what we want to do we want to change the mobile number to a null field so we'll take the same name of the column and then we'll call it nullable and then pass it as a change and in drop column we need to do exactly reverse we don't have to give this in the label but it actually a normal string field which is required so let's go and run this migration as this run or migration let's refresh this now if you see this mobile number is is nullable it's saying yes so this field becomes anna level right so we have already see how we can change the column change the column type change column name change the column default values so we have pretty much everything which we want to change with a column the next thing i'll explain you about the column modifiers so let's go in our laravel documentation and see what column modifier will have so if you see in the column modifier what it actually is it actually make the column some some what we we need to for example we have to give a column and by default nullable or for example we want to add a comment for a column we we make make like we have to assign unsigned right we need to give it as a default timestamp so all these column what modifier are actually available in our migration file so the number of column modifier are available in this laravel documentation you will see the inside this column modifier let's see we want to use uh some of them for the status column i want to give it a default one default value of this column should be one for that again i need to run a command which i'll say php addition make migration update status column in contacts table so what it does it actually generates a migration file you'll see this has the contacts the next thing what we need to do is table boolean status and i'll call it default 1 and change it right let's copy the same thing and put it here i don't want any default and in down function let's go ahead and migrate our it's run the migration if i go this now if you see this status column it has a default value of one so by this we can add the column modifier in our migration and use it the very next thing is the indexing as we all know the database indexes are very very important for us to create while creating any of the database so let's understand how we can create index and rename it and in any case we have to draw we need to drop that index as well let's see how we can create the index so there are plenty of methods available for indexes like primary index unique index normal index full text index right so these are the index available in migration for example i want to index a mobile number in our table right what i'll do again i'll create a migration file and give it a mobile number and let's open the migration and what i'll do i'll add a simple index here and name it as a mobile number in down function will drop this index so we have a function of drop index and i have to rename it as mobile number so what it does actually when we run the migrate it will add an index and when we roll back it actually remove this index let's run the migration go into our database right now if you see this index we only have a primary index on the id column let's refresh this you see we have another index which is on a mobile number so this is how we can add an index in our table for any column for renaming any index it's very simple we have this available method rename index where we have to pass from into it actually rename this index and for dropping any of the index we have plenty of methods available to drop a primary key we have draw primary to drop a unique key we have a drop unique and to drop any normal index we have drop index so we have these available indexes which we can use so we have available method to add index rename index drop index so by these all three we can add rename and drop indexes in laravel migration let's see the foreign key constraint as we all know the foreign key is the most important part of our database structure so whenever we add a foreign key constraint we can easily relate a database table to another table with this foreign key constraint so let's add in our contacts table a user id as we already have this users table available so let's add a constraint here in the contacts table for user id as a foreign key let's go and create a migration for this in a migration the very next thing which we have to give here so we'll name it a foreign id will name this column as user id the next thing which we want to give it a constraint and in the constraint we can pass this table users and we'll use this function cascade on delete update as well as cascade on delete right and in down function what we have to actually do here we need to drop this constraint for an id and the column name would be user id right let's save it migrate this as soon as you migrate this let's go in our database table refresh it all right as you see we have this user id and if you notice this foreign key constraint we have user id as well as here a new index for the foreign key has been added which is user id and it is referring to users table of id you see this uses of id so i think we have covered everything which we want to cover in our migration so the very basic things we have covered there are a lot of things to explore you can always go to this documentation given by laravel and you can explore all the parts given here on the migration i hope this video is informative for you we have seen all the parts of migration starting from creating a migration creating a database adding a column renaming it changing the column type adding a column modifier indexes and then foreign key constraints so we have covered the very basic structure of the laravel migration and how we can manage our database using the migration i hope this video is informative for you if you if you found this video informative please do like this video subscribe our channel to get more videos on laravel till the next video keep watching till the next video keep watching [Music] you
Info
Channel: TechTool India
Views: 3,560
Rating: undefined out of 5
Keywords: laravel 9, laravel migration, laravel, laravel 8, laravel 9 tutorial, migration, laravel 9 migration, migration in laravel, laravel tutorial, migrations, laravel migrations, laravel database migration, stub migrations laravel 9, laravel 9 crud, laravel 9 new features, laravel 9 new features about migration, laravel 8 migration, laravel 8 tutorial, migrations laravel, laravel table migration, laravel 8 migrations, laravel migration in hindi, refresh laravel migration
Id: EB2qi8o845k
Channel Id: undefined
Length: 21min 35sec (1295 seconds)
Published: Tue Jun 07 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.