rasyidly/laravel-model-query 问题修复 & 功能扩展

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

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

rasyidly/laravel-model-query

最新稳定版本:v1.1.0

Composer 安装命令:

composer require rasyidly/laravel-model-query

包简介

Advanced laravel dynamic model queries

README 文档

README

Advanced laravel dynamic model queries

Installation

Install this package with composer

composer require rasyidly/laravel-model-query

Usage

There are several features including this package. To use all features, use this as trait as following example.

use Rasyidly/ModelQuery/ModelQuery;

class User extends Model
{
    /**
     * This will load Loadable, Searchable, Sortable, and Trashable Traits in single load.
     */
    use ModelQuery;
}

OR, manually use single feature as trait

Just use feature/s as a trait to your Model, e.g.:

use Rasyidly/ModelQuery/Loadable/Loadable;
use Rasyidly/ModelQuery/Searchable/Searchable;

class User extends Model
{
    use Loadable, Searchable;
}

Features

There are main feature of this package features and how-to-use.

📃 Loadable

Loadable is a trait that functions to load eloquent relations based on the $loadable property defined in the Model, like this:

class Post extends Model
{
    use ModelQuery;

    public $loadable = [
        'author.profile', 'commentable'
    ];

    public function author (): BelongsTo { ... }
}

If the relationship is not found in the definition, it will not give an error. Unless you give the wrong definition of the relationship, it is fatal.

Then, for use in the Controller, as follows:

Eager loading

// posts?load=author,commentable

$query = ['author', 'commentable'];

$posts = Post::loadable($request->query('load'))->get();

This will produce a collection/object of Post with the relations author and commentable by eager loading. Make sure the query is separated with comma if load more than one relation, so that loadable accepts array parameters according to Loadable's terms.

Lazy loading

It's the same, the only difference is that it will be loaded when it becomes an object (not a query builder a.k.a. n+1)

// posts?load=author.profile

$post = Post::find(1);

$query = ['author.profile'];
$post = $post->loadable($request->query('load'));

This will load the author.profile relation as per the $loadable definition.

📃 Searchable

Searchable is a trait that functions to search for specific text based on the $searchable property defined in the Model, like this:

class Post extends Model
{
    use ModelQuery;

    public $searchable = [
        'title', 'description', 'author.name'
    ];

    public function author (): BelongsTo { ... }
}

With this searchable definition, in sequence, it will search for a collection of Posts where title like % ... %, and so on. You can also use a relationship like the example above, author.name, meaning it will look for name in the author relationship

Then, for use in the Controller, as follows:

// posts?search=Hello

$posts = Post::searchable($request->query('search'))->get();

The result of the syntax above is to search for posts in the column defined by $searchable, in this case it will search like "%Hello%" in the title or description column or name in the author relationship

📃 Sortable

The sortable feature is a method for sorting by columns defined in the $sortable property, with the addition of ,asc or ,desc.

class Post extends Model
{
    use ModelQuery;

    public $sortable = [
        'name'
    ];
}

An example of implementation in the Controller is as follows:

// posts?sort=name,asc

$posts = Post::sortable($request->query('sort'))->get();

Basically, sortable is exactly like Laravel's built-in orderBy method, only I combine the sorting method, whether ascending or descending. This trait only accepts two ascending or descending parameters, otherwise it will ignore this method, no error will be displayed.

📃 Trashable

This trait only works if the Model uses SoftDeletes

There are no additional special properties like in the previous trait. This will only add a special method namely trashable($default = 'without') which only accepts 3 parameters namely with, only, and without.

The implementation is as follows:

// posts?trash=only

$posts = Post::trashable($request->query('trash'))->get();

The result of the query and syntax is that it will only call onlyTrashed from Laravel SoftDeletes.

Contributing

Contributions are always welcome, just open issues, fork and/or pull your request! Up to you!

License

This library is open-source software licensed under the MIT

rasyidly/laravel-model-query 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 30
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

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