承接 asseco-voice/laravel-json-search 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

asseco-voice/laravel-json-search

Composer 安装命令:

composer require asseco-voice/laravel-json-search

包简介

Laravel JSON search

关键字:

README 文档

README

Laravel JSON search

This package exposes a jsonSearch method on Laravel Eloquent models providing a detailed DB search with JSON as input parameter.

It functions out-of-the-box automatically for all Eloquent models within the project. No additional setup is needed.

Installation

Install the package through composer. It is automatically registered as a Laravel service provider, so no additional actions are required.

composer require asseco-voice/laravel-json-search

Usage

Package provides few endpoints out of the box:

  • POST /api/search/{model} - for search
  • PUT /api/search/{model}/update - for mass update by query results
  • DELETE /api/search/{model} - for mass delete by query results

Model should be provided in standard Laravel notation (lowercase plural) in order to map it automatically (i.e. /api/search/contacts in order to search Contact model).

By default, App namespace is used, but you can change the defaults or add additional endpoints if you have need for that in the package configuration by either adding a direct model mapping in model_mapping key (taking precedence over other options), or adding additional values to models_namespaces array to make it more generic.

Description on how to use each of those are in the configuration file.

If out-of-the-box solutions don't suit you, feel free to implement the logic directly within your controller. For details check out custom endpoints section.

Following are some examples, however there is much more to the search package than just filtering by attributes.

For detailed engine usage and logic, refer to this readme.

Examples

POST

Call the endpoint providing the following JSON:

{
    "search": {
        "first_name": "=foo;bar;!baz",
        "last_name": "=test"
    }
}

This will perform a SELECT * FROM some_table WHERE first_name IN ('foo, 'bar') AND first_name not in ('baz') or last_name in ('test').

Additionally, you are able to provide append array to resolve your custom defined properties on a Laravel model which aren't listed in $appends array. I.e.

public function getSomeAttribute()
{
    return 'foo';
}

You can return it by using:

{
    "search": {
        "first_name": "=foo;bar;!baz",
        "last_name": "=test"
    },
    "append": ["some"]
}

It is possible to do 1-level nested appends like:

"append": ["relation1.append_attribute_for_r1"]

More than 1-level is not supported.

Do note that this is completely unoptimized and will potentially cause really slow executions. Use with caution.

PUT

Call the endpoint providing the following JSON:

{
    "search": {
        "first_name": "=foo;bar;!baz",
        "last_name": "=test"
    },
    "update": {
        "first_name": "new name"
    }
}

This will perform a SELECT * FROM some_table WHERE first_name IN ('foo, 'bar') AND first_name not in ('baz') or last_name in ('test'), and on the given result set it will perform a mass update giving a new name to every record retrieved

DELETE

{
    "search": {
        "first_name": "=foo;bar;!baz",
        "last_name": "=test"
    }
}

This will perform a DELETE FROM some_table WHERE first_name IN ('foo, 'bar') AND first_name not in ('baz') or last_name in ('test') doing a mass delete by given parameters.

Custom endpoints

It is possible to create a custom endpoint if the current setup does not suit you.

Search

  • Add route:
Route::post('search', 'ExampleController@search');
  • Call the method within the controller and provide it with input parameters from JSON body.
public function search(Request $request)
{
    return SomeModel::jsonSearch($request->all())->get();
}

Update

  • Add route:
Route::put('search/update', 'ExampleController@search');
  • Call the method within the controller and provide it with input parameters from JSON body.
public function search(Request $request)
{
    $search = SomeModel::jsonSearch($request->except('update'));

    if (!$request->has('update')) {
        throw new Exception('Missing update parameters');
    }

    $search->update($request->update);

    return $search->get();
}

Delete

  • Add route:
Route::delete('search', 'ExampleController@search');
  • Call the method within the controller and provide it with input parameters from JSON body.
public function search(Request $request)
{
    return SomeModel::jsonSearch($request->all())->delete();
}

Search favorites

Favorites enable you to save searches for a specific user.

Usage:

  1. Run php artisan migrate.
  2. Use through standard laravel API resource routes on /api/search-favorites URL.
  3. If you need to modify migrations publish the package and set runs_migrations property in the config file to false.
  4. Set authorizeResource in asseco-search to false if you do not want $this->authorizeResource(SearchFavorite::class) to be added in controller.

It is possible to extend the model used for search favorites and replace with your own. Make sure your model extends SearchFavorite and replace search_favorite_model key in the configuration with your model.

Search - paginated

POST  https://service.url/api/search-paginated/:model 

Request:

{
    "page": 2,
    "limit": 10,
    "search": [ .... ]
}

Response:

{
    "data": [ ... ],
    "metadata": {
        "page": 2,
        "limit": 10,
        "count": 10,
        "total_count": 145
    }
}

Debugging

If you'd like to see query called instead of a result, uncomment dump line within Asseco\JsonSearch\SearchServiceProvider.

Due to Laravel query builder inner workings, this will not dump the resulting query for relations. For that purpose I'd recommend using Laravel query log.

Extending the package

Publishing the configuration will enable you to change package models as well as controlling how migrations behave. If extending the model, make sure you're extending the original model in your implementation.

asseco-voice/laravel-json-search 适用场景与选型建议

asseco-voice/laravel-json-search 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.61k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2020 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 asseco-voice/laravel-json-search 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-07-19