PHPackages                             orzlee/api-assistant - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. orzlee/api-assistant

ActiveLibrary

orzlee/api-assistant
====================

Help you quickly rebuild laravel api application

v2.1.0(5y ago)020MITPHPPHP &gt;=7.0.0

Since Apr 17Pushed 5y ago1 watchersCompare

[ Source](https://github.com/jianminLee/api-assistant)[ Packagist](https://packagist.org/packages/orzlee/api-assistant)[ RSS](/packages/orzlee-api-assistant/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (1)Versions (14)Used By (0)

### Installation

[](#installation)

安装包:

```
composer require orzlee/api-assistant
```

发布配置文件

```
php artisan vendor:publish --provider="Orzlee\ApiAssistant\ApiAssistantServiceProvider"

```

### Configurations

[](#configurations)

修改`app\Exceptions\Handler.php`以接管API异常处理:

```
    use Orzlee\ApiAssistant\Exceptions\ApiExceptionReport;

    public function render($request, Throwable $exception)
    {
        $reporter = ApiExceptionReport::make($exception);
        if ($reporter->shouldCatchException()) {
            return $reporter->report();
        }
        return parent::render($request, $exception);
    }
```

修改控制器文件

```
use Orzlee\ApiAssistant\Traits\ApiResponse;
use Orzlee\ApiAssistant\Traits\Controller\QueryRequestFilter;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
    use ApiResponse, QueryRequestFilter;
}
```

修改模型文件

```
use Orzlee\ApiAssistant\Traits\Model\AutoLoadRelation;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use AutoLoadRelation;

    /**
     * 允许过滤查询的字段，条件字段会匹配该字段，自动过滤掉不包含字段
     * 条件过滤查询也会通过该字段过滤
     * api http://xxx.com/your-api-url?fields=name,created_at&name=test
     * @return string[]
     */
    public static function fields() :array
    {
        return [
            'name',
            'avatar',
            'created_at',
        ];
    }

    /**
     * api url 参数中包含fields=my_post会自动加载关系
     * api http://xxx.com/your-api-url?fields=name,my_post
     * @return string[]
     */
    public static function relationFields() :array
    {
        return [
            'posts' => 'my_post',
        ];
    }

    public function posts()
    {
        return $this->hasMany(Post::class);
    }

}
```

在`config/api-assistant.php`中还有很多配置。

### Use

[](#use)

在控制器中使用:

```
class PostController extends Controller{

    public function __construct(Request $request) {
        $this->init(Post::class, $request);
    }

    public function index(Request $request)
    {
        return $this->success(PostResource::collection($this->get()));
    }

    public function show($id)
    {
        return $this->success(new PostResource($this->query->findOrFail($id)));
    }

    public function update($id, CreatePostRequest $request)
    {
        $post = $this->query->where('user_id', $request->user()->id)->find($id);
        if (!$post) {
            return $this->failed('not found post.', 404);
        }
        $post->update($request->all());
        return $this->success(new PostResource($post));
    }
}
```

查询例子:

```
http://xxx.com/your-api-url?fields=id,name,status,created_at&status=!=,1&name=!=,null&orderBy=status,desc&page_size=2&page=2
http://xxx.com/your-api-url?name=like,~test~
http://xxx.com/your-api-url?status=1,or,2
http://xxx.com/your-api-url?status[]=1,or,2&status
//闭包查询，查询sql像这样： select * form post where (status = -1 or state = -2) and name = 'test' order by status;
http://xxx.com/your-api-url?closures[][status]=or,-1&closures[][state]=or,-2&name=test&orderBy=status

```

模型缓存:

```
use \App\Traits\Model\HasCache;
use \Illuminate\Database\Eloquent\Model;
class Category extends Model
{
    use HasCache;

    /**
     * 自定义缓存和key
     * @return mixed
     */
    public static function getParentCategoryForCache()
    {
        return self::cache('parent_category', function (Category $model) {
            return $model->where('parent_id', 0)->get();
        });
    }
    /**
     * 不传入key值，使用默认key，但是会替换cache函数缓存
     * @return mixed
     */
    public static function getSubCategoryForCache()
    {
        return self::cache(function (Category $model) {
            return $model->where('parent_id', '>', 0)->get();
        });
    }

}
use Illuminate\Routing\Controller;
use \Orzlee\ApiAssistant\Traits\ApiResponse;
class CategoryController extends Controller
{
    use ApiResponse;
    public function categories()
    {
        return $this->success(Category::cache());
        //return $this->success(Category::getParentCategoryForCache());
        //清理缓存
        //(new Category)->forgetCache();
        //(new Category)->forgetCache('parent_category');
    }
}
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~20 days

Recently: every ~2 days

Total

13

Last Release

1982d ago

Major Versions

v0.0.6 → v1.0.02020-10-20

v1.0.0 → v2.0.32020-12-08

### Community

Maintainers

![](https://www.gravatar.com/avatar/5481547edb8c902069d93510a7b80b05ff7f1c8c0fe8de6e9d01c129e4bc3688?d=identicon)[jianminLee](/maintainers/jianminLee)

---

Top Contributors

[![jianminLee](https://avatars.githubusercontent.com/u/13827117?v=4)](https://github.com/jianminLee "jianminLee (28 commits)")

### Embed Badge

![Health badge](/badges/orzlee-api-assistant/health.svg)

```
[![Health](https://phpackages.com/badges/orzlee-api-assistant/health.svg)](https://phpackages.com/packages/orzlee-api-assistant)
```

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11120.2M21](/packages/anourvalar-eloquent-serialize)[namu/wirechat

A Laravel Livewire messaging app for teams with private chats and group conversations.

54324.5k](/packages/namu-wirechat)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135192.6k5](/packages/statamic-rad-pack-runway)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
