This tutorial on several ways to create a database in CodeIgniter 4 is a very effective tutorial that will teach you how to create databases using the Database Forge Class and the Spark command.
Database administration is a critical component of developing powerful web apps as a CodeIgniter developer. In this tutorial, we will look at different approaches and methods for creating databases in CodeIgniter 4, allowing you to set up your database with ease and efficiency.
Read More: How To Integrate ChatGPT API in CodeIgniter 4 Tutorial
This tutorial will give you with step-by-step instructions and best practises whether you prefer utilising the command-line interface, database management tools, or programmatically constructing databases.
Let’s get started.
CodeIgniter 4 Installation
To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.
composer create-project codeigniter4/appstarter codeigniter-4
Assuming you have successfully installed application into your local system.
Environment (.env) Setup
When we install CodeIgniter 4, we will have env file at root. To use the environment variables means using variables at global scope we need to do env to .env
Either we can do via renaming file as simple as that. Also we can do by terminal command.
Open project in terminal
cp env .env
Above command will create a copy of env file to .env file. Now we are ready to use environment variables.
Enable Development Mode
CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.
Open .env file from root.
# CI_ENVIRONMENT = production
// Do it to
CI_ENVIRONMENT = development
Now application is in development mode.
Loading Database Forge Class
The Database Forge Class contains methods that help you manage your database.
If you work with Custom Library / Custom Helper then at first you need to create an instance of Database Forge class.
Load the Forge Class as follows:
$forge = \Config\Database::forge();
If the database you wish to manage is not the default one, you may alternatively supply another database group name to the DB Forge loader:
$this->myforge = \Config\Database::forge('other_db');
Read More: How To Cache Database Query Using CodeIgniter Cache?
Create Database Using Database Forge Class
You can access createDatabase method using Forge class instance. This will take database name as a parameter value and help you to create it.
$forge->createDatabase('my_db')
It Returns true/false based on success or failure.
Example
Let’s create a migration file and by which we will create database.
php spark make:migration create_my_database
It will create xxx_CreateMyDatabase.php inside app/Database/Migrations folder.
<?php namespace App\Database\Migrations; use CodeIgniter\Database\Migration; class CreateMyDatabase extends Migration { public function up() { if($this->forge->createDatabase("myDb")){ // Success message }else{ // Failure message } } public function down() { // } }
Next, run this command to execute this Migration file.
php spark migrate
It will create myDb database in your given connection values of PhpMyAdmin.
An optional second parameter set to true will add IF EXISTS statement or will check if a database exists before create it (depending on DBMS).
$forge->createDatabase('my_db', true);
Create Database Using Spark CLI Command
You can create database by using a special command of Spark CLI.
Back to project terminal and run this command.
php spark help db:create
Example
php spark help db:create myDb
This will do the same functionality what we seen inside first case of database creation.
If you are on a testing environment or you are using the SQLite3 driver, you may pass in the file extension for the file where the database will be created using the –ext option
php spark db:create myDb --ext sqlite
Note*: While doing this operation make sure your application first connected with Database Driver.
More Learning
To drop a database from your connected database driver, you can use this dropDatabase() of Database Forge class.
You can access to this method by creating an instance of it.
$forge->dropDatabase('myDb')
That’s it.
We hope this article helped you to learn about Methods To Create Database in CodeIgniter 4 Tutorial in a very detailed way.
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.