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.