PHPackages                             immachakata/ci4-relationships - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. immachakata/ci4-relationships

ActiveLibrary[Database &amp; ORM](/categories/database)

immachakata/ci4-relationships
=============================

Easily add and manage database relationships in CodeIgniter 4

v1.1(2y ago)47981MITPHPPHP ^7.4 || ^8.0

Since Sep 25Pushed 1y agoCompare

[ Source](https://github.com/im-machakata/ci4-relationships)[ Packagist](https://packagist.org/packages/immachakata/ci4-relationships)[ RSS](/packages/immachakata-ci4-relationships/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (3)Dependencies (1)Versions (4)Used By (0)

CodeIgniter 4 Database Relationships
====================================

[](#codeigniter-4-database-relationships)

A simple solution to add relationships to your Codeigniter 4 application.

Installation
------------

[](#installation)

You can install this library from composer by running `composer install immachakata/ci4-relationships`

Defining Relationships
----------------------

[](#defining-relationships)

You can define as many relationships as you want using the `hasMany` or `hasOne` functions as needed. Here's an example of defining a single relationship for a users avatar and multiple relationship for a users links.

```
namespace App\Models;

use CodeIgniter\Model;
use CI4Extensions\Database\RelationshipsTrait;

class UserModel extends Model
{
    use RelationshipsTrait;

    ...
    // declare relationships
    public function initialize(){
        $this->hasOne('avatar', AvatarModel::class, 'user_id');
        $this->hasMany('links', LinkModel::class, 'user_id');
    }
}
```

Using Relationships
-------------------

[](#using-relationships)

When you perfom your query, explicitly mention the field name you want back as you defined in the initialize function. To do so, use the `with($fieldName)` function. Here's an example of retrieving the user avatar

```
class UserController extends BaseController
{
    public function index(){
        ...
        $this->respond([
            'user' => model(UserModel::class)->with('avatar')->findAll()
        ]);
    }
}
```

Credits
-------

[](#credits)

Forked from [michalsn/codeigniter-nested-model](https://github.com/michalsn/codeigniter-nested-model)

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance27

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 62.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~158 days

Total

3

Last Release

688d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/108731754?v=4)[Isaac Machakata](/maintainers/immachakata)[@immachakata](https://github.com/immachakata)

---

Top Contributors

[![michalsn](https://avatars.githubusercontent.com/u/459185?v=4)](https://github.com/michalsn "michalsn (5 commits)")[![immachakata](https://avatars.githubusercontent.com/u/108731754?v=4)](https://github.com/immachakata "immachakata (3 commits)")

### Embed Badge

![Health badge](/badges/immachakata-ci4-relationships/health.svg)

```
[![Health](https://phpackages.com/badges/immachakata-ci4-relationships/health.svg)](https://phpackages.com/packages/immachakata-ci4-relationships)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
