cybercog / simple-eloquent

Adaptation of eloquent for retrieving its attributes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Simple eloquent extension for Laravel

Latest Version Software License Quality Score Coverage Status

This extension presents some methods for eloquent ORM in order to reduce time and memory consuming. Sometimes application doesn't need all of eloquent overhead. It just requires to retrieve model's attributes with relations and nothing more. In this case this methods might be enough useful for you.

Extension supports:

- eloquent relations
- illuminate pagination
- PDO::FETCH_OBJ and PDO::FETCH_ASSOC fetch modes

Requirements

laravel >= 5.3

Instalation

Run

composer require --prefer-dist volosyuk/simple-eloquent "*"

or add this code line to the require section of your composer.json file:

"volosyuk/simple-eloquent": "*"

Usage

Just use SimpleEloquent trait in your model. If you want to get models with relations attach SimpleEloquent trait to related models as well.

use Volosyuk\SimpleEloquent\SimpleEloquent;
class Department extends \Illuminate\Database\Eloquent\Model
{
    use SimpleEloquent;
}

Then use getSimple()(or another available) method instead of get. All of available methods have the same signature as their default analogues. They have the same name but with Simple suffix.

$users = User::whereHas('units')->withCount('units')->with('units')->limit(10)->getSimple()

List of available methods

Profit

This extesion was tested on real project.

Exapmle 1 - users with details; 50 per page
$users = User::with([
    'units.leaders.performance',
    'teams.leaders.performance',
    'programs.courses'
])->limit(50)->get()
Time Memory consumption
get() 0.62s 6.0mb
getSimple() 0.19s 3.0mb
Exapmle 2 - select models with 5-level relation
$goals = Goal::with('goalUser.user.courses.points.user')->limit(20)->get()
Time Memory consumption
get() 1.48s 28.5mb
getSimple() 0.47s 15.5mb
Exapmle 3 - let's select 1000 models
$performance = Performance::whereHas('user')->with('goal.goalUser')->limit(1000)->get()
Time Memory consumption
get() 0.22s 2.0mb
getSimple() 0.06s 1.1mb

What do you loose?

Since this extension provides less expensive methods you'll definitely loose some functionality. Basic methods return collection of eloquent models in contrast to new additional methods which return collection of stdClasses|arrays. This exapmle will show the difference between results.

$categories = Category::with('articles')->get() // want to grab all categories with articles
Method get returns
Illuminate\Database\Eloquent\Collection::__set_state([
    'items' => [
        0 => Category::__set_state([
            'guarded' => [],
            'connection' => 'default',
            'table' => NULL,
            'primaryKey' => 'id',
            'keyType' => 'int',
            'incrementing' => true,
            'with' => [],
            'withCount' => [],
            'perPage' => 15,
            'exists' => true,
            'wasRecentlyCreated' => false,
            'attributes' => [
                'id' => '1',
                'name' => 'Test category',
                'created_at' => '2017-12-21 12:33:34',
                'updated_at' => '2017-12-21 12:33:34'
            ],
            'original' => [
                'id' => '1',
                'name' => 'Test category',
                'created_at' => '2017-12-21 12:33:34',
                'updated_at' => '2017-12-21 12:33:34',
            ],
            'changes' => [],
            'casts' => [],
            'dates' => [],
            'dateFormat' => NULL,
            'appends' => [],
            'dispatchesEvents' => [],
            'observables' => [],
            'relations' => [
                'articles' => Illuminate\Database\Eloquent\Collection::__set_state([
                    'items' => [
                        0 => Article::__set_state([
                            'guarded' => [],
                            'connection' => 'default',
                            'table' => NULL,
                            'primaryKey' => 'id',
                            'keyType' => 'int',
                            'incrementing' => true,
                            'with' => [],
                            'withCount' => [],
                            'perPage' => 15,
                            'exists' => true,
                            'wasRecentlyCreated' => false,
                            'attributes' => [
                                'id' => '1',
                                'category_id' => '1',
                                'title' => 'Test article',
                                'created_at' => '2017-12-21 12:33:34',
                                'updated_at' => '2017-12-21 12:33:34',
                            ],
                            'original' => [
                                'id' => '1',
                                'category_id' => '1',
                                'title' => 'Test article',
                                'created_at' => '2017-12-21 12:33:34',
                                'updated_at' => '2017-12-21 12:33:34',
                            ],
                            'changes' => [],
                            'casts' => [],
                            'dates' => [],
                            'dateFormat' => NULL,
                            'appends' => [],
                            'dispatchesEvents' => [],
                            'observables' => [],
                            'relations' => [],
                            'touches' => [],
                            'timestamps' => true,
                            'hidden' => [],
                            'visible' => [],
                            'fillable' => [],
                        ]),
                    ],
                ]),
            ],
            'touches' => [],
            'timestamps' => true,
            'hidden' => [],
            'visible' => [],
            'fillable' => [],
        ])
    ]
]);
Method getSimple returns
Illuminate\Support\Collection::__set_state([
    'items' => [
        0 => stdClass::__set_state([
            'id' => '1',
            'name' => 'Test category',
            'created_at' => '2017-12-21 12:43:44',
            'updated_at' => '2017-12-21 12:43:44',
            'articles' => Illuminate\Support\Collection::__set_state([
                'items' => [
                    0 => stdClass::__set_state([
                        'id' => '1',
                        'category_id' => '1',
                        'title' => 'Test article',
                        'created_at' => '2017-12-21 12:43:44',
                        'updated_at' => '2017-12-21 12:43:44',
                    ]),
                ],
            ]),
        ]),
    ]
]);

Since you'll get stdClasses|arrays you won't reach out to casting, appends, guarded/fillable, crud and another possibilities. That's why new methods are much faster 😏

About

Adaptation of eloquent for retrieving its attributes

License:MIT License


Languages

Language:PHP 100.0%