Inside this article we will see the concept of database seeding in CodeIgniter 4 using json file. CodeIgniter 4 database seeding from json file is technique to dump test data into tables in bulk.
This tutorial will be super easy to understand and it’s steps are easier to implement in your code as well. Database seeding is the process in which we feed test data to tables. We can insert data either using Faker library, manual data or means of some more data sources like CSV, JSON.
CodeIgniter 4 Database Seeding from CSV File Tutorial, Click here.
In this tutorial we will use JSON file to seed data into database table using CodeIgniter 4.
Learn More –
- CodeIgniter 4 Send Email with SMTP Tutorial
- CodeIgniter 4 Send Push Notification to IOS Using Firebase
- CodeIgniter 4 Server Side DataTable Tutorial
- CodeIgniter 4 Server Side DataTable Using SSP Library
Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.
Let’s get started.
Download & Install CodeIgniter 4 Setup
We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed.
Here are the following ways to download and install CodeIgniter 4 –
- Manual Download
- Composer Installation
- Clone Github repository of CodeIgniter 4
Complete introduction of CodeIgniter 4 basics — Click here to go. After going through this article you can easily download & install setup.
Here is the command to install via composer –
$ composer create-project codeigniter4/appstarter codeigniter-4
Assuming you have successfully installed application into your local system.
Turn Development Mode On
When we install CodeIgniter 4, we have env file at root. To use the environment variables means using variables at global scope we need to do env to .env
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.
CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.
# CI_ENVIRONMENT = production// Do it toCI_ENVIRONMENT = development
Now application is in development mode.
To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.
CREATE DATABASE codeigniter4_app;
Successfully, we have created a database.
Let’s connect with the application.
Database Connectivity to Application
Open .env file from project root. Search for DATABASE. You should see the connection environment variables.