PHPackages                             leinonen/yii2-eloquent - 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. leinonen/yii2-eloquent

AbandonedArchivedYii2-extension[Database &amp; ORM](/categories/database)

leinonen/yii2-eloquent
======================

Drop in implementation of the Illuminate Database component for Yii2

0.5.6(10y ago)21.8k3[1 issues](https://github.com/lordthorzonus/yii2-eloquent/issues)MITPHP

Since Dec 8Pushed 10y ago1 watchersCompare

[ Source](https://github.com/lordthorzonus/yii2-eloquent)[ Packagist](https://packagist.org/packages/leinonen/yii2-eloquent)[ RSS](/packages/leinonen-yii2-eloquent/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (8)Versions (4)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/a050a329a5fdad842f643b1427df902917c7fccf66f72b98ca75523d06f5fd8c/68747470733a2f2f706f7365722e707567782e6f72672f6c65696e6f6e656e2f796969322d656c6f7175656e742f762f737461626c65)](https://packagist.org/packages/leinonen/yii2-eloquent) [![Total Downloads](https://camo.githubusercontent.com/e1e978904cea92252c901dbe604c8c31f56bec2a0217681749710005af709df1/68747470733a2f2f706f7365722e707567782e6f72672f6c65696e6f6e656e2f796969322d656c6f7175656e742f646f776e6c6f616473)](https://packagist.org/packages/leinonen/yii2-eloquent) [![Latest Unstable Version](https://camo.githubusercontent.com/c97dee02c4b32e52c38dabc9e94133c88f7a388c0f12bddbfea300ac5f66f445/68747470733a2f2f706f7365722e707567782e6f72672f6c65696e6f6e656e2f796969322d656c6f7175656e742f762f756e737461626c65)](https://packagist.org/packages/leinonen/yii2-eloquent) [![License](https://camo.githubusercontent.com/d1543915db2c1cb543c804cbd9bfb00f555fcebcb82f50b8ba3636cbb40f1b7a/68747470733a2f2f706f7365722e707567782e6f72672f6c65696e6f6e656e2f796969322d656c6f7175656e742f6c6963656e7365)](https://packagist.org/packages/leinonen/yii2-eloquent) [![Build Status](https://camo.githubusercontent.com/634f24e2c434822cf1917655c27c4fbaf13fb6ec7c36970f9494bc9cba7e3096/68747470733a2f2f7472617669732d63692e6f72672f6c6f726474686f727a6f6e75732f796969322d656c6f7175656e742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/lordthorzonus/yii2-eloquent) [![SensioLabsInsight](https://camo.githubusercontent.com/5e153331d88a3b8f965bc8a3d1c6930645794a1a179e43beea5c5d0c7cd4bc33/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f32366562613530342d363534612d343230622d626636362d3539343737336232303231382f6d696e692e706e67)](https://insight.sensiolabs.com/projects/26eba504-654a-420b-bf66-594773b20218)

Yii2-eloquent
=============

[](#yii2-eloquent)

A drop in Laravels Eloquent and Illuminate/Database implementation for Yii2

Features:
---------

[](#features)

- Working extension (Still need to check Eloquent events and pagination)
- Migrations
- Fixtures
- Yii style model validation for Eloquent models
- Ability to feed Eloquent models to ActiveForm widget
- Model factories for use with testing instead of Fixtures?
- Other Laravel test helpers for Eloquent models?
- Adapter for Yii::$app-&gt;db? It's confusing to use it now in IDE with autocompletion and creating an own Yii base class is too much work
- Better Docs

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

[](#installation)

Require this package, with [Composer](https://getcomposer.org/), in the root directory of your project.

```
composer require leinonen/yii2-eloquent
```

### Configuration:

[](#configuration)

To configure the package just override and bootstrap your Yii db component in the application config.

```
use leinonen\Yii2Eloquent\Yii2Eloquent;
...
'bootstrap' => ['db'],
'components' => [
    'db' => [
        'class' => Yii2Eloquent::class,
        'driver' => 'mysql',
        'database' => 'yii2basic',
        'prefix' => '',
        'host' => 'localhost',
        'username' => 'root',
        'password' => 'secret',
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
    ];
```

Usage:
------

[](#usage)

Like said, by default the package overrides Yii's database component. So one you can access `Illuminate\Database\Capsule\Manager` like this:

```
use Illuminate\Database\Schema\Blueprint;

Yii::$app->db->schema()->create('user', function (Blueprint $table) {
    $table->increments('id');
    $table->string('email')->unique();
    $table->timestamps();
});
```

However its much more preferable to access the capsule with static methods or inject it via dependency injection. IDEs cannot autocomplete accessing the db component from the Yii god object.

```
use Illuminate\Database\Capsule\Manager as Capsule;

$users = Capsule::table('users')->where('votes', '>', 100)->get();

$results = Capsule::select('select * from users where id = ?', array(1));
```

For complete docs please refer to: [Illuminate\\Database](https://github.com/illuminate/database) and [Laravel](http://laravel.com/docs/master/database)

### Eloquent

[](#eloquent)

Using Eloquent is covered in [Laravels documentation](http://laravel.com/docs/master/eloquent). However this package provides also an altenrative base model which to extend form. By extending from `leinonen\Yii2Eloquent\Eloquent\Model` instead of `Illuminate\Database\Eloquent\Model` you'll get some Yii functionalities to your Eloquent models. For example you can then declare `rules()` `scenarios()` etc. as you are used with Yiis AR models. These can then be also fed to ActiveForm widget, validated using `$model->validate()` etc.

Simple example of Eloquent Model:

```
use leinonen\Yii2Eloquent\Eloquent\Model;

class Order extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'order';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name'];

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            ['address', 'required'],
            ['address', 'string', 'min' => 3],
        ];
    }
}
```

The package also provides a simple trait to make your Eloquent user model compatible with Yii's IdentityInterface. Just `use leinonen\Yii2Eloquent\Eloquent\AuthenticatableTrait;`

Migrations
----------

[](#migrations)

If you want to use Yiis basic migration commands you have overwrite the default migration controller in the console app config.

```
use leinonen\Yii2Eloquent\Migrations\MigrateController;

'controllerMap' => [
      'migrate' => [
            'class' => MigrateController::class,
      ],
],
```

Then all the commands `php yii migrate/create` etc. will work as expected. Note that you have to use the `Capsule`in migrations instead of refering to Yiis base migration class:

```
use Illuminate\Database\Capsule\Manager as Capsule;
use yii\db\MigrationInterface;

class myMigration implements MigrationInterface
{
    public function up()
    {
        Capsule::schema()->create('my_table', function($table){
            $table->increments('id');
        });
    }

    public function down()
    {
        Capsule::schema()->dropIfExists('my_table');
    }
}
```

Fixtures
--------

[](#fixtures)

If you like Yiis fixtures no worries! Just extend `leinonen\Yii2Eloquent\Fixtures\EloquentFixture` when creating the Fixture class and everything will work as usual:

```
use leinonen\Yii2Eloquent\Fixtures\EloquentFixture;

class OrderFixture extends EloquentFixture
{
    public $modelClass = Order::class;

    public function getData()
    {
        return [
            'example1' => [
                'address' => 'Test address',
            ],
        ];
    }
}
```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance10

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~1 days

Total

3

Last Release

3807d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b6a5d10ce7b5286c44580e2437115587c0930424cfbaa20cf397011837987fac?d=identicon)[lordthorzonus](/maintainers/lordthorzonus)

---

Top Contributors

[![lordthorzonus](https://avatars.githubusercontent.com/u/8689671?v=4)](https://github.com/lordthorzonus "lordthorzonus (44 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/leinonen-yii2-eloquent/health.svg)

```
[![Health](https://phpackages.com/badges/leinonen-yii2-eloquent/health.svg)](https://phpackages.com/packages/leinonen-yii2-eloquent)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[dyrynda/laravel-cascade-soft-deletes

Cascading deletes for Eloquent models that implement soft deletes

1.2k3.1M5](/packages/dyrynda-laravel-cascade-soft-deletes)[tightenco/parental

A simple eloquent trait that allows relationships to be accessed through child models.

1.5k1.8M5](/packages/tightenco-parental)[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8703.0M17](/packages/yajra-laravel-oci8)

PHPackages © 2026

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