PHPackages                             agungsugiarto/codeigniter4-repository - 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. agungsugiarto/codeigniter4-repository

ActiveLibrary

agungsugiarto/codeigniter4-repository
=====================================

Implementation of repository pattern for CodeIgniter 4. The package allows out-of-the-box filtering of data based on parameters in the &lt;b&gt;request&lt;/b&gt;, and also allows you to quickly integrate the list filters and custom criteria.

v1.1.0(5y ago)101.1k5MITPHPPHP ^7.3 || 8.0

Since Aug 16Pushed 2y ago1 watchersCompare

[ Source](https://github.com/agungsugiarto/codeigniter4-repository)[ Packagist](https://packagist.org/packages/agungsugiarto/codeigniter4-repository)[ RSS](/packages/agungsugiarto-codeigniter4-repository/feed)WikiDiscussions master Synced yesterday

READMEChangelog (10)Dependencies (3)Versions (14)Used By (0)

CodeIgniter4 Repository Pattern
===============================

[](#codeigniter4-repository-pattern)

[![Latest Stable Version](https://camo.githubusercontent.com/c30bfa0640de26595217e01f770b69a5b20f98e7b3f4214c22122fbe38c38127/68747470733a2f2f706f7365722e707567782e6f72672f6167756e67737567696172746f2f636f646569676e69746572342d7265706f7369746f72792f76)](https://github.com/agungsugiarto/codeigniter4-repository/releases)[![Total Downloads](https://camo.githubusercontent.com/f6e45420cd3767fb0e78bfbbfcae5e4abfaea0348e0672384572c9a8315ad2a4/68747470733a2f2f706f7365722e707567782e6f72672f6167756e67737567696172746f2f636f646569676e69746572342d7265706f7369746f72792f646f776e6c6f616473)](https://packagist.org/packages/agungsugiarto/codeigniter4-repository)[![Latest Unstable Version](https://camo.githubusercontent.com/9551324f6894bba20816a39e0593c73713492e9e62acd5850369e6d4310823d8/68747470733a2f2f706f7365722e707567782e6f72672f6167756e67737567696172746f2f636f646569676e69746572342d7265706f7369746f72792f762f756e737461626c65)](https://packagist.org/packages/agungsugiarto/codeigniter4-repository)[![License](https://camo.githubusercontent.com/c9eb6b41ca591ecdf47a1ac17e608ddc25f03fbe22d5871fa7138eea62b09820/68747470733a2f2f706f7365722e707567782e6f72672f6167756e67737567696172746f2f636f646569676e69746572342d7265706f7369746f72792f6c6963656e7365)](https://packagist.org/packages/agungsugiarto/codeigniter4-repository)

About
-----

[](#about)

Implementation of repository pattern for CodeIgniter 4. The package allows out-of-the-box filtering of data based on parameters in the request, and also allows you to quickly integrate the list filters and custom criteria.

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Configuration](#configuration)
- [Overview](#overview)
- [Usage](#usage)
    - [Create a Model](#create-a-model)
    - [Create a Repository](#create-a-repository)
    - [Use built-in methods](#use-built-in-methods)
    - [Create a Criteria](#create-a-criteria)
    - [Scope, Filter, and Order](#scope-filter-and-order)

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

[](#installation)

Via Composer

```
$ composer require agungsugiarto/codeigniter4-repository
```

Overview
--------

[](#overview)

##### Package allows you to filter data based on incoming request parameters:

[](#package-allows-you-to-filter-data-based-on-incoming-request-parameters)

```
https://example.com/news?title=Title&custom=value&orderBy=name_desc

```

It will automatically apply built-in constraints onto the query as well as any custom scopes and criteria you need:

```
protected $searchable = [
    // where 'title' equals 'Title'
    'title',
];

protected $scopes = [
    // and custom parameter used in your scope
    'custom' => MyScope::class,
];
```

```
class MyScope extends ScopeAbstract
{
    public function scope($builder, $value, $scope)
    {
        return $builder->where($scope, $value)->orWhere(...);
    }
}
```

Ordering by any field is available:

```
protected $scopes = [
    // orderBy field
    'orderBy' => OrderByScope::class,
];
```

Package can also apply any custom criteria:

```
return $this->news->withCriteria([
    new MyCriteria([
        'category_id' => '1',
        'name' => 'Name',
        ['created_at', '>', Time::now()],
    ]),
    ...
])->get();
```

Usage
-----

[](#usage)

### Create a Model

[](#create-a-model)

Create your model:

```
namespace App\Models;

use CodeIgniter\Model;

class News extends Model
{
    ...
}
```

### Create a Repository

[](#create-a-repository)

Extend it from `Fluent\Repository\Eloquent\BaseRepository` and provide `entity()` method to return full model class name:

```
namespace App;

use App\Models\News;
use Fluent\Repository\Eloquent\BaseRepository;

class NewsRepository extends BaseRepository
{
    public function entity()
    {
        // Whatever choose one your style.

        return new News();
        // or
        return 'App\Models\News';
        // or
        return News::class;
    }
}
```

### Use built-in methods

[](#use-built-in-methods)

```
use App\NewsRepository;

class NewsController extends BaseController
{
    protected $news;

    public function __construct()
    {
        $this->news = new NewsRepository();
    }
    ....
}
```

### Available methods

[](#available-methods)

- Execute the query as a "select" statement or get all results:

```
/**
 * Get method implement parameter "select", "limit" and "offset".
 * The default will be select * and return all offset data.
 *
 * Example: $this->news->get(['*'], 50, 100);
 */
$news = $this->news->get();
```

- Execute the query and get the first result:

```
$news = $this->news->first();
```

- Find a model by its primary key:

```
$news = $this->news->find(1);
```

- Add basic where clauses and execute the query:

```
$news = $this->news->findWhere([
        // where id equals 1
        'id' => '1',
        // other "where" operations
        ['news_category_id', '
