yepwoo/laravel-parser

轻松从前端加载模型关系/配件/属性,无需更改API

dev-main 2022-06-02 16:06 UTC

This package is auto-updated.

Last update: 2024-09-30 01:46:40 UTC


README

Software License

安装

composer require yepwoo/laravel-parser

用法

允许您的应用程序在无需更改API控制器的情况下加载任何模型 关系 & 属性。将逗号分隔的关系/属性传递到您的HTTP请求中。

加载关系

with 参数添加到您的HTTP请求中

/posts?with=comments,tags,anyOtherRelation

加载属性/访问器

append 参数添加到您的HTTP请求中

/posts?append=my_custom_attribute

如何使用

  • 前往您的控制器并调用 use Yepwoo\LaravelParser\LoadParser
  • LoadParser 类创建新对象,并传递 requestdata -> new LoadParser($request, $data)
  • 如果您想加载 关系,请使用 loadRelation 函数。
  • 如果您想加载 属性/访问器,请使用 loadAttributes 函数。

以下是一个完整示例

<?php
namespace App\Http\Controllers;

use App\Http\Resources\PostResource;
use App\Models\Post;
use Illuminate\Http\Request;
use Yepwoo\LaravelParser\LoadParser;

class PostController extends Controller
{

    public function index(Request $request)
    {
        $posts = Post::query()
        // .. your query logic here
        ->get();
        
        // new instance
        $parser = new LoadParser($request, $posts);

        // load relations
        $parser->loadRelations();
        
        // load attributes
        $parser->loadAttributes();

        return PostResource::collection($posts);
    }
}

响应示例

[
    {
        "id": 1,
        "title": "Aperiam.",
        "comments": [
            {
                "id": 6,
                "body": "Consequuntur dolores voluptates qui minima. Enim modi quam perferendis iste eum.",
                "post_id": 1,
                "created_at": "2022-06-02T15:03:36.000000Z",
                "updated_at": "2022-06-02T15:03:36.000000Z"
            },
            {
                "id": 8,
                "body": "Eaque quaerat nobis voluptatum fugiat a quisquam. Qui consequuntur officia eum ut blanditiis.",
                "post_id": 1,
                "created_at": "2022-06-02T15:03:36.000000Z",
                "updated_at": "2022-06-02T15:03:36.000000Z"
            }
        ],
        "custom_attr": "post_1"
    },
    {
        "id": 2,
        "title": "Incidunt.",
        "comments": [
            {
                "id": 5,
                "body": "Sequi beatae atque placeat excepturi distinctio. Illum et eum quisquam natus quaerat qui eos.",
                "post_id": 2,
                "created_at": "2022-06-02T15:03:36.000000Z",
                "updated_at": "2022-06-02T15:03:36.000000Z"
            },
            {
                "id": 7,
                "body": "Corrupti eveniet enim et. Qui veritatis consectetur voluptas. Natus esse et rem aut.",
                "post_id": 2,
                "created_at": "2022-06-02T15:03:36.000000Z",
                "updated_at": "2022-06-02T15:03:36.000000Z"
            },
            {
                "id": 12,
                "body": "Perferendis fugit harum magnam et fugiat id beatae. Sit sed ipsam omnis magnam vel aut.",
                "post_id": 2,
                "created_at": "2022-06-02T15:03:36.000000Z",
                "updated_at": "2022-06-02T15:03:36.000000Z"
            }
        ],
        "custom_attr": "post_2"
    }
]

资源

将很快发布帖子与视频。

待办事项

支持筛选器