ervinsvilumsons/laravel-eloquentql
Composer 安装命令:
composer require ervinsvilumsons/laravel-eloquentql
包简介
Dynamically optimize Eloquent queries and API resources with JSON:API sparse fieldsets and includes.
关键字:
README 文档
README
A small Laravel package to extend Eloquent models with JSON:API sparse fieldsets, include relation loading, and resource filtering.
Usage
- Add the trait to your Eloquent model:
use ErvinsVilumsons\LaravelEloquentQL\Concerns\HasEloquentQL; class Post extends Model { use HasEloquentQL; }
- Use the
jsonApiquery scope with request input:
$posts = Post::query() ->jsonApi() ->get();
- Send JSON:API-style request parameters:
fields[posts]=title,bodyinclude=author,comments
This will select only the requested post fields and eager load the requested relationships.
Resource filtering
You can filter resource output so only requested attributes and included relations are returned.
Standard Laravel resource example
use ErvinsVilumsons\LaravelEloquentQL\EloquentQL; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; class PostResource extends JsonResource { public function toArray(Request $request): array { $resource = [ 'id' => $this->id, 'title' => $this->title, 'description' => $this->description, 'author' => new AuthorResource($this->whenLoaded('author')), ]; return EloquentQL::filterResource($resource, 'posts', $request); } }
If the request uses fields[posts]=id,title and include=author, the response will only include id, title, and author.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-17