I'm trying to get the columns name from "users" table in my database using detFieldNames function in codeigniter 4 but something went wrong. Here is the error: Error
Model class:
class UsersModel extends Model
{
public function __construct()
{
$db = \Config\Database::connect();
$this->builder = $db->table('users');
}
function getColumns()
{
$names = $this->db->getFieldNames('users');
foreach ($names as $field) {
echo $field;
}
exit;
}
You don't have to manually set/configure the "Query Builder object". Simply set the
$tableprotected property on the model to represent the name of the database table.UsersModel.php
UserController.php
Addendum
You could alternatively just use the
db_connect()helper method to get the default "database connection". I.e:$names = db_connect()->getFieldNames("users")