volosyuk / simple-eloquent
eloquent的属性检索适配
8.0
2021-06-22 17:23 UTC
Requires
- php: ^7.2
- illuminate/database: ^8.0
- illuminate/pagination: ^8.0
- illuminate/support: ^8.0
Requires (Dev)
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-09-19 08:36:04 UTC
README
本扩展提供了一些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()
示例 2 - 选择具有5级关系的模型
$goals = Goal::with('goalUser.user.courses.points.user')->limit(20)->get()
示例 3 - 让我们选择1000个模型
$performance = Performance::whereHas('user')->with('goal.goalUser')->limit(1000)->get()
您会失去什么?
由于这个扩展提供了更经济的方法,您肯定会失去一些功能。基本方法返回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以及其他可能性。这就是为什么新方法要快得多 😏