activix/simple-eloquent

此包已被废弃,不再维护。未建议替代包。

eloquent的属性检索适配

10.0.0 2023-07-20 19:00 UTC

This package is auto-updated.

Last update: 2023-10-20 19:35:11 UTC


README

Latest Version Software License Quality Score Coverage Status Total Downloads

本扩展提供了一些eloquent ORM的方法,以减少时间和内存消耗。有时应用程序不需要eloquent的所有开销。它只需要检索模型的相关属性。在这种情况下,这些方法可能对您非常有用。

扩展支持

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

需求

laravel >= 5.3

安装

运行

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

或将以下代码行添加到您的composer.json文件的require部分

"volosyuk/simple-eloquent": "*"

用法

只需在您的模型中使用SimpleEloquent特质。如果您想获取具有关系的模型,请将SimpleEloquent特质附加到相关模型上。

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

然后使用方法调用链中的simple()方法。

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

好处

示例 1 - 带详细信息的用户;每页 50 个
$users = User::with([
    'units.leaders.performance',
    'teams.leaders.performance',
    'programs.courses'
])->limit(50)->get()
时间 内存消耗
get() 0.62 秒 6.0 MB
simple()->get() 0.19 秒 3.0 MB
示例 2 - 选择具有 5 级关系的模型
$goals = Goal::with('goalUser.user.courses.points.user')->limit(20)->get()
时间 内存消耗
get() 1.48 秒 28.5 MB
simple()->get() 0.47 秒 15.5 MB
示例 3 - 选择 1000 个模型
$performance = Performance::whereHas('user')->with('goal.goalUser')->limit(1000)->get()
时间 内存消耗
get() 0.22 秒 2.0 MB
simple()->get() 0.06 秒 1.1 MB

您会失去什么?

由于此扩展提供了更便宜的方法,您肯定会失去一些功能。基本方法返回eloquent模型集合,而新的功能返回stdClasses|数组集合。此示例将显示结果之间的差异。

$categories = Category::with('articles')->get() // want to grab all categories with articles
get()返回
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()返回
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',
                    ]),
                ],
            ]),
        ]),
    ]
]);

由于您将获得stdClasses|数组,您将无法访问类型转换、附加、守卫/填充、CRUD和其他可能性。这就是为什么新方法要快得多 😏