定制 emeguan/laravel-resources 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

emeguan/laravel-resources

Composer 安装命令:

composer require emeguan/laravel-resources

包简介

Adds extra functionality to the Laravel Eloquent: API Resources, allowing you to choose attributes and relationships to return via url

关键字:

README 文档

README

Adds extra functionality to the Laravel Eloquent: API Resources, allowing you to choose attributes and relationships to return via url

Introduction

When we create a JSON API from Laravel we can do it in many ways, the most used way (I think) is to create a Rest API. We can use the json:api specification if we want or use one of the great libraries already available for Laravel such as:

But it is possible that our API with the standard methods of a Rest API is not enough and we prefer to have extra parameters with which to perform different queries on the data before returning it in JSON format.
In this case we can generate our queries with Eloquent and return the data with the toJson method.
With the use of the Laravel Eloquent: API Resources we can control which attributes and relationships we want to return for each model in a static way. With this small library that we have here we can modify the API Resources dynamically from parameters in the url.

Instalation

Install using Composer

composer require emeguan/laravel-resources

Requirements

I have not tested it but it should work with any version of Laravel that includes Eloquent API Resources, that is, Laravel 5.5 and later.
The code has been tested with Laravel 9

Use

Model

class A extends Model
{
    use HasFactory;
    use \EmeGuan\Resources\ModelTrait;  //<--

Controller

Route::get('/as', function ()
{
    $as=A::all();
    return new \EmeGuan\Resources\Collection($as);
});

Route::get('/a/{id}', function ($id)
{
    $a=A::find($id);
    return new \EmeGuan\Resources\Resource($a);  //<--
});

Call

http://laravel-resource.local/as

{
  "data": [
    {
      "id": 1,
      "name": "A1",
      "created_at": "2023-02-06 16:55:52",
      "updated_at": "2023-02-06 16:55:52"
    },
    {
      "id": 2,
      "name": "A2",
      "created_at": "2023-02-06 16:55:52",
      "updated_at": "2023-02-06 16:55:52"
    },

http://laravel-resource.local/a/1

{
  "data": {
    "id": 1,
    "name": "A1",
    "created_at": "2023-02-06 16:55:52",
    "updated_at": "2023-02-06 16:55:52"
  }
}

Documentation

In the url we can specify the attributes we want from a model and the relationships to include. Let's see this with an example.
We start from these 5 models:

class A extends Model
{
    use HasFactory;
    use ModelTrait;

    protected $fillable = [ 'id', 'name'];

    public function bs()
    {
        return $this->hasMany(B::class);
    }
    
    public function ds()
    {
        return $this->hasMany(D::class);
    }
}

class B extends Model
{
    use HasFactory;
    use ModelTrait;

    protected $fillable = [ 'id', 'name', 'a_id'];

    public function cs()
    {
        return $this->hasMany(C::class);
    }

    public function a()
    {
        return $this->belongsTo(A::class);
    }
}

class C extends Model
{
    use HasFactory;
    use ModelTrait;

    protected $fillable = [ 'id', 'name', 'b_id'];

    public function b()
    {
        return $this->belongsTo(B::class);
    }
}

class D extends Model
{
    use HasFactory;
    use ModelTrait;

    protected $fillable = [ 'id', 'name', 'a_id'];

    public function es()
    {
        return $this->hasMany(E::class);
    }

    public function a()
    {
        return $this->belongsTo(A::class);
    }
}

class E extends Model
{
    use HasFactory;
    use ModelTrait;

    protected $fillable = [ 'id', 'name', 'd_id'];

    public function d()
    {
        return $this->belongsTo(D::class);
    }
}
{
  "data": [
    {
      "id": 1,
      "name": "A1"
    },
    {
      "id": 2,
      "name": "A2"
    },
    {
      "id": 3,
      "name": "A3"
    },
{
  "data": [
    {
      "id": 1,
      "name": "A1",
      "bs": [
        {
          "name": "B1"
        },
        {
          "name": "B2"
        },
        {
          "name": "B3"
        }
      ]
    },
    {
      "id": 2,
      "name": "A2",
      "bs": [
        {
          "name": "B4"
        },
        {
          "name": "B5"
        },
        {
          "name": "B6"
        }
      ]
    },
    {
      "id": 3,
      "name": "A3",
      "bs": [
        {
          "name": "B7"
        },
        {
          "name": "B8"
        }
      ]
    },
{
  "data": [
    {
      "id": 1,
      "name": "A1",
      "bs": [
        {
          "name": "B1",
          "cs": [
            {
              "id": 1,
              "name": "C1",
              "b_id": 1,
              "created_at": "2023-02-06 16:57:18",
              "updated_at": "2023-02-06 16:57:18"
            },
            {
              "id": 2,
              "name": "C2",
              "b_id": 1,
              "created_at": "2023-02-06 16:57:18",
              "updated_at": "2023-02-06 16:57:18"
            },
            {
              "id": 3,
              "name": "C3",
              "b_id": 1,
              "created_at": "2023-02-06 16:57:18",
              "updated_at": "2023-02-06 16:57:18"
            }
          ]
        },
        {
          "name": "B2",
          "cs": [
            {
              "id": 4,
              "name": "C4",
              "b_id": 2,
              "created_at": "2023-02-06 16:57:18",
              "updated_at": "2023-02-06 16:57:18"
            },
            {
              "id": 5,
              "name": "C5",
              "b_id": 2,
              "created_at": "2023-02-06 16:57:18",
              "updated_at": "2023-02-06 16:57:18"
            },
            {
              "id": 6,
              "name": "C6",
              "b_id": 2,
              "created_at": "2023-02-06 16:57:18",
              "updated_at": "2023-02-06 16:57:18"
            }
          ]
        },
        {
          "name": "B3",
          "cs": [
            {
              "id": 7,
              "name": "C7",
              "b_id": 3,
              "created_at": "2023-02-06 16:57:18",
              "updated_at": "2023-02-06 16:57:18"
            },
            {
              "id": 8,
              "name": "C8",
              "b_id": 3,
              "created_at": "2023-02-06 16:57:18",
              "updated_at": "2023-02-06 16:57:18"
            }
          ]
        }
      ]
    },
    {
      "id": 2,
      "name": "A2",
      "bs": [

If attributes are not specified all are returned.
All relations are included with the dot notation, in the example it is not necessary to specify include=bs,bs.cs

{
  "data": [
    {
      "name": "B1",
      "a": {
        "id": 1,
        "name": "A1"
      }
    },
    {
      "name": "B2",
      "a": {
        "id": 1,
        "name": "A1"
      }
    },
    {
      "name": "B3",
      "a": {
        "id": 1,
        "name": "A1"
      }
    },
{
  "data": [
    {
      "id": 1,
      "name": "B1",
      "a": {
        "id": 1,
        "name": "A1"
      }
    },
    {
      "id": 2,
      "name": "B2",
      "a": {
        "id": 1,
        "name": "A1"
      }
    },
    {
      "id": 3,
      "name": "B3",
      "a": {
        "id": 1,
        "name": "A1"
      }
    },
{
  "data": [
    {
      "id": 1,
      "name": "A1",
      "created_at": "2023-02-06 16:55:52",
      "updated_at": "2023-02-06 16:55:52",
      "bs": [
        {
          "name": "B1"
        },
        {
          "name": "B2"
        },
        {
          "name": "B3"
        }
      ],
      "ds": [
        {
          "name": "D1"
        },
        {
          "name": "D2"
        },
        {
          "name": "D3"
        }
      ]
    },
    {
      "id": 2,
      "name": "A2",
      "created_at": "2023-02-06 16:55:52",
      "updated_at": "2023-02-06 16:55:52",
      "bs": [
        {
          "name": "B4"
        },
        {
          "name": "B5"
        },
        {
          "name": "B6"
        }
      ],
      "ds": [
        {
          "name": "D4"
        },
        {
          "name": "D5"
        },
        {
          "name": "D6"
        }
      ]
    },
{
  "data": [
    {
      "id": 1,
      "name": "A1"
    },
    {
      "id": 2,
      "name": "A2"
    },
    {
      "id": 3,
      "name": "A3"
    },
    {
      "id": 4,
      "name": "A4"
    },
    {
      "id": 5,
      "name": "A5"
    },

TODO

{
  "data": [
    {
      "id": 1,
      "bs": [
        {
          "cs": [
            {
              "name": "C1"
            },
            {
              "name": "C2"
            },
            {
              "name": "C3"
            }
          ]
        },
        {
          "cs": [
            {
              "name": "C4"
            },
            {
              "name": "C5"
            },
            {
              "name": "C6"
            }
          ]
        },
        {
          "cs": [
            {
              "name": "C7"
            },
            {
              "name": "C8"
            }
          ]
        }
      ]
    },

Something similar to this could be returned where the 1st level relationship did not appear

{
  "data": [
    {
      "id": 1,
      "cs": [
        {
          "name": "C1"
        },
        {
          "name": "C2"
        },
        {
          "name": "C3"
        },
        {
          "name": "C4"
        },
        {
          "name": "C5"
        },
        {
          "name": "C6"
        },
        {
          "name": "C7"
        },
        {
          "name": "C8"
        }
      ]
    },
  • ...

License

Laravel API Resources is open-source software licensed under the MIT license.

emeguan/laravel-resources 适用场景与选型建议

emeguan/laravel-resources 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 35 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 02 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「api」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 emeguan/laravel-resources 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 emeguan/laravel-resources 我们能提供哪些服务?
定制开发 / 二次开发

基于 emeguan/laravel-resources 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 35
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-06