Models provide a way to interact with a specific table in your database. They come out of the box with helper methods for much of the standard ways you would need to interact with a database table, including finding records, updating records, deleting records, and more.
<?php
namespace App\Models;
use CodeIgniter\Model;
class UserModel extends Model
{
}
protected $userModel;
public function __construct()
{
$this->userModel = model('App\Models\UserModel');
$db = db_connect('seller');
$this->userModel = model('Usermodel', true, $db);
}
protected $table = 'users';
protected $returnType = 'array';
protected $allowedFields = ['id','name'];
Find function uses single row to match. This function checks the argument at the primary key. And it returns the value in the object or array format.
public function find_data()
{
$result = $this->userModel->find(1);
echo "<pre>";
print_r($result);
}
public function find_data()
{
$result = $this->userModel->find([1,3]);
echo "<pre>";
print_r($result);
}
To fetch the values of a column, we use the Find column function.
public function find_column()
{
$result =
$this->userModel->findColumn('salary');
echo "<pre>";
print_r($result);
}
public function find_all()
{
$result = $this->userModel->findAll();
echo "<pre>";
print_r($result);
}
public function find_all()
{
$result = $this->userModel
->where('id',2)
->findAll();
echo "<pre>";
print_r($result);
}
public function find_all()
{
$result = $this->userModel
->where('id!=',2)
->findAll();
echo "<pre>";
print_r($result);
}
If the first record of the table has to be fetched, then we use the first function.
public function find_first()
{
$result =
$this->userModel->first();
echo "<pre>";
print_r($result);
}
public function find_first()
{
$result =
$this->userModel
->where('id>',2)
->first();
echo "<pre>";
print_r($result);
}
My name is Deepak tailor as a fullstack developer. I have been in the IT industry (PHP, Nodejs, flutter) for the last 5 years. For professional and customize web development & app development, you can send inquiry on our email.
----
You can contact him at deepaktailor10@yahoo.in