What is CI4 BaseModel usage patterns?

Why Interviewers Ask This

Advanced questions like this reveal whether a candidate has internalized CodeIgniter deeply enough to make architectural decisions. Strong answers demonstrate both breadth and depth of experience.

Answer

Creating a custom BaseModel in CI4 that all your models extend is a powerful pattern for sharing common behaviour. Create app/Models/BaseModel.php extending CodeIgniter\Model. Add common functionality: soft deletes enabled by default (protected $useSoftDeletes = true), timestamps enabled (protected $useTimestamps = true), common validation patterns (reusable rule sets), shared callbacks (e.g., always trim string fields before insert), common scopes (active(), orderByLatest()), and audit fields (automatically set created_by from the logged-in user ID in a beforeInsert callback). Add helper methods: findOrFail($id) throws 404 instead of returning null, paginator($perPage) returns paginated results with consistent defaults. All application models extend BaseModel instead of CI4's Model. This reduces repetition across models and ensures consistent behaviour like soft deletes and audit trails without having to remember to configure them in each model.

Pro Tip

If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.