CI 4 Insert Row in Database

To use models, first we need to create an instance of that.

Here is the way to create.

# Load model

use App\Models\StudentModel;

# Creating model instance

$object = new StudentModel();

# Insert Row
$object->insert([ ... ])

Next, we can call method for insert() using $object.

<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use App\Models\StudentModel;

class StudentController extends BaseController
{
	public function insertData()
	{
		$object = new StudentModel();

		$object->insert([
			"name" => "Sanjay Kumar",
			"email" => "test@gmail.com",
			"mobile" => "9879638521"
		]);

		//...
	}
}

Above code will insert a row inside associated table for StudentModel.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more