kobykorman/eloquentify
最新稳定版本:v1.0.0
Composer 安装命令:
composer require kobykorman/eloquentify
包简介
Easily transform complex custom query results into fully hydrated hierarchical Eloquent models.
README 文档
README
Eloquentify for Laravel
👎 Lazy Loading (N+1 queries)
😑 Eager Loading (R+1 queries)
😎 Greedy Loading (1 query)
Why Eloquentify?
⚡ Single Query: Replace N+1/R+1 queries with one efficient query
💯 Eloquent Models: Get real Eloquent models, not plain objects
🔗 Nested Relations: Support for complex hierarchies of any depth
✨ Clean API: Maintain Laravel's elegant syntax
Using Eloquent can be costly in terms of how many queries are fired behind the scenes when a model has many relationships. What if we could leverage the database for what it was meant for while retaining the Eloquent experience?
Eloquentify easily transforms the result of a single custom query into nested Eloquent models, so you can continue enjoying the Eloquent API and all of its benefits.
Installation
composer require kobykorman/eloquentify
Quick Start
1. Add the trait:
// App\Models\Model use Illuminate\Database\Eloquent\Model as BaseModel; class Model extends BaseModel { use EloquentifiesQueries; }
2. Write the query:
// App\Models\User class User extends Model { public static function getById($id) { // 1 custom query $result = DB::table('users') ->select('...') ->join('person...') ->join('team...') // ... ->where('id', $id) ->get()
3. Transform the result:
// feed the result and the relations hierarchy
// and get them all properly hydrated and nested
return User::eloquentify($result, [
Person::class
Team::class,
Role::nest(Permission::nest(
Resource::class,
Ability::class
))
Post::nest(Comment::class)
])
}
}
4. Enjoy:
// App\Controllers\UserController $userName = User::getById(1)->person->name
Requirements
- PHP 8.0 or higher
- Laravel 9.0 or higher
Models:
- Must follow Laravel naming conventions (e.g.
User,Post,UserProfile- notuser,Posts,user_profile) - Must be instantiatable without parameters (e.g.
new User(),new Post()- notnew User($data)) - All relation methods must exist on the model (e.g.
Usermodel must haveposts()method)- Relation method names must match either:
- Singular method names are assumed to be "one" (e.g.
profile(),author(),category()) - Plural method names are assumed to be "many" (e.g.
posts(),comments(),tags())
- Singular method names are assumed to be "one" (e.g.
- Relation method names must match either:
Query:
- Must include all related models' primary keys and the columns for the desired attributes
- Related model columns must be snake_cased prefixed, e.g.:
(SELECT users.id, users.name, posts.id AS post_id, posts.title AS post_title)
- Results must be provided as a Laravel Collection (
DB::table()...->get())
License
This library is open-sourced software licensed under the MIT license.
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-04
