Launch offer
Business website $150 USD Custom plugin $200 USD Ready in 5 days
Get a quote
CodeIgniter

Models in CodeIgniter

Use Models to encapsulate table access, allowed fields, and timestamps.

Models in CodeIgniter — a practical guide to CodeIgniter models with clear examples you can reuse in real projects.

CodeIgniter Tutorial Series (35/74). Prefer one article? Read the complete CodeIgniter tutorial.

Short description

CI4 models provide `find`, `save`, `insert`, `update`, and `delete` with protection via `$allowedFields`.

Basic model

<?php
namespace AppModels;
use CodeIgniterModel;

class PostModel extends Model
{
    protected $table = 'posts';
    protected $primaryKey = 'id';
    protected $allowedFields = ['title', 'body', 'user_id'];
    protected $useTimestamps = true;
}

Leave a reply

Your email address will not be published. Required fields are marked *