Using Migrations to create table in database always considered a good practice when you do work in a team. Creating tables manually ( not using migration files) not a good practice as of now, When yo...
Gurpreet Kait
Author
Using Migrations to create table in database always considered a good practice when you do work in a team. Creating tables manually ( not using migration files) not a good practice as of now, When you do have functionality like migration. If you are working with CodeIgniter usign migrations in CodeIgniter 4 will make you more smarter.
You don't need to learn SQL to put all this stuff there in form of core PHP. It makes creating tables too easy.
Today we are going to learn migrations in CodeIgniter.
What is migrations in CodeIgniter
Migrations makes easy to create tables in database and which helps to handle tables programmatically and obviously you don't need to learn about SQL statements to build a table. As I said when you do work with a team then you can easily modify tables and play with the database.
Create table in CodeIgniter using migrations
Now, we are going to create our first table using CodeIgniter migrations.
Overview : We will create a "users" table first and then we will create a "product" table in which we will store products and we will also be using Foreign keys for relation. So, that we can identify the owner of the product. Let's begin.
Create A Database
Let's I will create a database called "shopkeeper" and will create both of the tables in this database.
You can create this database table manually or as you wish. After that we will go to Database.php file in Config directory and do configuration.
You just have to fill the hostname,username,password and database for now. And it will work.
Now, you can create other tables in the same way. If you do have any question please don't forget to put in the comment box and let's connect over twitter: Gurpreet kait
Conclusion
Creating table using migration is the best way to make it more flexible. If you are working on your side with a team it becomes easy to modify and discuss over database infrastructure using migration files.
It has some short of Commands to navigate easily.
command line help
Use php spark migrate to migrate the files or tables
Use php spark migrate:rollback to delete all the migrations
Use php spark migrate:refresh to remigrate all the migrations
Use php spark migrate:status to see the migration status.