2017 © Pedro Peláez
 

library simple-eloquent

Adaptation of eloquent for retrieving its attributes

image

volosyuk/simple-eloquent

Adaptation of eloquent for retrieving its attributes

  • Tuesday, May 22, 2018
  • by andreyvolosyuk
  • Repository
  • 0 Watchers
  • 35 Stars
  • 2,020 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 24 Versions
  • 71 % Grown

The README.md

Simple eloquent extension for Laravel

Latest Version Software License Quality Score Coverage Status Total Downloads, (*1)

This extension presents some methods for eloquent ORM in order to reduce time and memory consumption. 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.

, (*2)

Extension supports:

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

Requirements

laravel >= 5.3

Installation

Run, (*3)

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

or add this code line to the require section of your composer.json file:, (*4)

"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., (*5)

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

Then use simple() method in chain of methods calls., (*6)

$users = User::whereHas('units')->withCount('units')->with('units')->limit(10)->simple()->get();
$activeUser = User::simple()->where('is_active', 1)->first();

Profit

Example 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
simple()->get() 0.19s 3.0mb
Example 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
simple()->get() 0.47s 15.5mb
Example 3 - let's select 1000 models
$performance = Performance::whereHas('user')->with('goal.goalUser')->limit(1000)->get()
Time Memory consumption
get() 0.22s 2.0mb
simple()->get() 0.06s 1.1mb

What do you lose?

Since this extension provides less expensive methods you'll definitely lose some functionality. Basic methods return collection of eloquent models in contrast to new functionality which returns collection of stdClasses|arrays. This example will show the difference between results., (*7)

$categories = Category::with('articles')->get() // want to grab all categories with articles
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' => [],
        ])
    ]
]);
simple()->get() 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 :smirk:, (*8)

The Versions

22/05 2018

dev-master

9999999-dev

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

22/03 2018

dev-five_three

dev-five_three

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

22/03 2018

5.3.4

5.3.4.0

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

22/03 2018

dev-five_four

dev-five_four

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

22/03 2018

5.4.4

5.4.4.0

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

22/03 2018

dev-five_five

dev-five_five

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

22/03 2018

5.5.4

5.5.4.0

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

22/03 2018

5.6.4

5.6.4.0

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

20/03 2018

5.6.3

5.6.3.0

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

20/03 2018

5.5.3

5.5.3.0

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

20/03 2018

5.4.3

5.4.3.0

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

20/03 2018

5.3.3

5.3.3.0

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

16/03 2018

5.6.2

5.6.2.0

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

11/03 2018

5.5.2

5.5.2.0

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

11/03 2018

5.4.2

5.4.2.0

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

11/03 2018

5.3.2

5.3.2.0

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

11/03 2018

5.6.1

5.6.1.0

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

26/02 2018

5.6.0

5.6.0.0

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

06/12 2017

5.4.1

5.4.1.0

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

06/12 2017

5.3.1

5.3.1.0

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

06/12 2017

5.5.1

5.5.1.0

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

17/11 2017

5.5.0

5.5.0.0

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

16/11 2017

5.4.0

5.4.0.0

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent

16/11 2017

5.3.0

5.3.0.0

Adaptation of eloquent for retrieving its attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrey Volosyuk

laravel eloquent