ervinsvilumsons/laravel-eloquentql 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

  1. Add the trait to your Eloquent model:
use ErvinsVilumsons\LaravelEloquentQL\Concerns\HasEloquentQL;

class Post extends Model
{
    use HasEloquentQL;
}
  1. Use the jsonApi query scope with request input:
$posts = Post::query()
    ->jsonApi()
    ->get();
  1. Send JSON:API-style request parameters:
  • fields[posts]=title,body
  • include=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
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-06-17